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 happens if main method is not static?

498


What is jit compiler ?

578


Write a program to reverse array in place?

592


What do you understand by casting in java language?

580


What is formatted output in java?

501






What is difference between static class and singleton pattern?

480


What are classloaders?

599


What are the java ide’s?

575


What is keyword in oop?

524


What is oop in java?

529


What is scanner in java?

543


What is a generic code?

528


What is singleton class and how can we make a class singleton?

661


Is string thread safe in java?

581


What is the use of conditional statement?

564