Can an abstract class have a constructor?
Answer / 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 |
what is a thread?
How do you clear a list in java?
How to call one constructor from the other constructor ?
What is bigger kb or mb?
Which collection allows duplicate values in java?
What is the byte order of byte buffer?
What is the difference between method and means?
Hi Friends, can you give difference between extending thread class and implementing runnable interface.
Explain OOPs concept.
What is var keyword ?
What modifiers are used for interface declaration?
Explain about wait() method?