Can you have a constructor in abstract class?
Answer Posted / jag bhushan
yes,
we can have constructor in abstract class.
But we can not make instance of the abstract class.
instead we can make a reference to that abstract class.
and when we make a new object of the class which extends
the abstract class, the constructor of abstract class get
called.
see the code for example:
public abstract class TestAbstract {
TestAbstract(){
System.out.println("...in abstract class'
constructor");
}
public abstract void showAbstract();
public void show(){
System.out.println("...in show");
}
}
public class Test extends TestAbstract{
public static void main(String[] args) {
TestAbstract ta = new Test(); //
constructor call
ta.showAbstract();
ta.show();
}
public void showAbstract() {
System.out.println("...in showAbstract");
}
}
| Is This Answer Correct ? | 22 Yes | 3 No |
Post New Answer View All Answers
How do you make an arraylist empty in java?
What does provide mean construction?
Can we create a class inside a class in java?
How do generics work in java?
Why strings in java are called as immutable?
What are nested classes in java?
Why does java not support operator overloading?
What is space character in java?
What do you mean by ordered and sorted in collections in java?
How do you declare an array that will hold more than 64KB of data?
How many threads can I run java?
What is use of valueof () in java?
What are the new features in java 8? Explain
I want to persist data of objects for later use. What’s the best approach to do so?
What is integer parseint?