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 |
Can we write method inside a method in java?
What is a void return type?
How do you compare two strings lexicographically?
What is a variable and constant?
What do you mean by platform independence?
What is the difference between variable declaration and variable initialization?
What is a string what operation can be performed out with the help of a string?
How a string is stored in memory?
How do you escape in java?
What is the difference between final, finally and finalize() in java?
Explain throw keyword in java?
Why is java architectural neutral?