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 is jpa specification?
Explain java coding standards for methods?
What is mvc in java?
How does split work in java?
Can we assign integer value to char in java?
Which method must be implemented by all threads?
What is final int?
Can we declare a class as static?
Why heap memory is called heap?
What is diamond operator in java?
What is sortedmap in java?
What does java edition mean?
How we can execute any code even before main method?
Give a brief description of java socket programming?
Can we change the scope of the overridden method in the subclass?