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
What is super in java?
What is linked hashmap and its features?
What are constructors in java?
What is a return in java?
Which method you will use to create a new file to store some log data. Each time a new log entry is necessary, write string to the file in java ?
What is threaded programming and when is it used? : Java thread
Can we serialize arraylist in java?
What is anti pattern in programming?
What is difference between path and classpath variables?
What is the difference between final, finally and finalize() in java?
What are the main features of java?
What is object of class in java?
Explain covariant method overriding in java.
How do you bind variables?
Why we use set in java?