Can an abstract class have a constructor?
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(); // onstructor
call
ta.showAbstract();
ta.show();
}
public void showAbstract() {
System.out.println("...in showAbstract");
}
}
| Is This Answer Correct ? | 70 Yes | 2 No |
Post New Answer View All Answers
List out benefits of object oriented programming language?
How do you empty a list in java?
Explain wait(), notify() and notifyall() methods of object class ?
What is the public field modifier?
What do you understand by weak reference?
Explain java code for recursive solution's base case?
How can constructor chaining be done using this keyword?
Explain illegalmonitorstateexception and when it will be thrown?
What is the equal sign?
Can we use synchronized block for primitives?
What are java packages? What is the significance of packages?
What is final method in java?
Can singleton class be serialized?
What is the exception hierarchy in java?
How many bits is a float?