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
Tell me about different OOPS concepts.
Explain access specifiers?
What differences exist between iterator and listiterator?
What is anagram of a string?
What are implicit objects in java?
What are the four versions of java?
What is functional interface in java example?
Why bytecode is called bytecode?
What is the base class of all exception classes?
What is methods in java?
What do you mean by singleton class in java?
What are basic data types?
What is the largest long allowed by java?
What is string literal in java?
Is boolean a data type in java?