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

Can we have static methods in an interface?

758


How to solve the problem of generating the unique hash keys with hash function?

1638


What are the advantages and disadvantages of object cloning?

745


What is nested class?

731


What is the purpose of format function?

811






What is entry set in java?

719


Which is better stringbuffer or stringbuilder?

702


What is the difference amongst jvm spec, jvm implementation, jvm runtime ?

693


How many decimal digits is 64 bit?

687


Is Java a dying language?

775


How can you make sure that your singleton class will always return single instance in multi-threaded environment?

764


Write a regular expression to validate a password. A password must start with an alphabet and followed by alphanumeric characters; its length must be in between 8 to 20.

779


how to know the total memory occupied by the objects in the ArrayList(Array list may contain duplicate objects)

2093


Difference between static binding and dynamic binding?

761


What is import java util arraylist?

843