Can you have a constructor in abstract class?
Answer Posted / vinoth kumar
Yes,
we can define it in the abstract class itself.But it invoke
only by super() in sub class constructor.
Example:
abstract class AbsConstCheck{
AbsConstCheck(){
System.out.println("I AM WORKING AbsConstCheck");
}//some other methods declaration
}
class Sub extends AbsConstCheck{
sub(){
super();//calling abstract class constructor
System.out.println("I AM WORKING Sub");
}
}
class Main{
public static void main(String vin[]){
Sub s=new Sub();//calling sub,abstract class constructor
}
}
Output:
I AM WORKING AbsConstCheck
I AM WORKING Sub
| Is This Answer Correct ? | 1 Yes | 0 No |
Post New Answer View All Answers
Can a constructor call the constructor of parent class?
What is difference between length and length() method in java ?
How to perform quicksort in java?
What are the 3 types of loops in java?
Explain about member inner classes?
Which way a developer should use for creating thread, i.e. Sub classing thread or implementing runnable.
What is a local class in java?
What is the difference between error and an exception?
What is compareto () in java?
What is the default execution method in java?
What is jit and its use?
How do you make an arraylist empty in java?
v-model life cycle
Explain some best practices you would apply while using collection in java?
What is functional interface in javatpoint?