Can you have a constructor in abstract class?
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(); //
constructor call
ta.showAbstract();
ta.show();
}
public void showAbstract() {
System.out.println("...in showAbstract");
}
}
| Is This Answer Correct ? | 22 Yes | 3 No |
Post New Answer View All Answers
Explain different forms of polymorphism?
why we use merge option in hybernate pls give a ex snippet
When arithmeticexception is thrown?
In a container there are 5 components. I want to display all the component names, how will you do that?
State the significance of public, private, protected class?
What is difference between null and void?
What is annotation in java?
What is the range of the short type?
What are the 7 types of characters?
how to one war file class to another war file class?
What is the use of jtable?
What is the difference between an interface and an abstract class?
What is final int?
What is the use of System class?
What things should be kept in mind while creating your own exceptions in java?