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
Is map sorted in java?
Why char array is favored over string for the storage of passwords?
How does the java compiler work?
What is the difference between length and length() method in java?
What does ide stand for?
What is identifier with example?
how to create multithreaded program? Explain different ways of using thread? When a thread is created and started, what is its initial state? : Java thread
What is the difference between hashset and treeset in java?
Difference between Linked list and Queue?
What is constructor and its types?
What is final method in java?
In which order the iterator iterates over collection?
Name some OOPS Concepts in Java?
What is the difference between the prefix and postfix forms of the ++ operator?
What is string args [] in java?