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


Please Help Members By Posting Answers For Below Questions

What is the difference between compare and compareto in java?

481


which one is more efficient int x; 1. if(x==null) 2.if(null==x) state which one either 1 or 2?

9148


What is the type of lambda expression?

531


Why do we need data serialization?

534


How many bytes is double?

535






Is a method a function?

533


How do you override a variable in java?

516


What is jagged array in java?

515


Write a java program to check if a number is prime or not?

528


Is java programming easy?

526


What makes a function well defined?

500


Can we declare array without size in java?

537


What do negative exponents mean?

536


What is the use of bufferedreader?

519


What is a java object and java application?

565