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
What are constructors in java?
Tell some latest versions in JAVA related areas?
Which class is the superclass for all the classes?
Difference between throw and throws?
What is binary tree in java?
How do you delete a list in java?
Compare overloading and overriding?
What are the access modifiers available in java?
What is Java Shutdown Hook?
What is a local class in java?
Explain the reason behind ending a program with a system.exit(0)?
What does n mean?
What is purpose of applet programming?
What is a void method?
Can static methods be inherited?