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 we have static methods in an interface?
How to solve the problem of generating the unique hash keys with hash function?
What are the advantages and disadvantages of object cloning?
What is nested class?
What is the purpose of format function?
What is entry set in java?
Which is better stringbuffer or stringbuilder?
What is the difference amongst jvm spec, jvm implementation, jvm runtime ?
How many decimal digits is 64 bit?
Is Java a dying language?
How can you make sure that your singleton class will always return single instance in multi-threaded environment?
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.
how to know the total memory occupied by the objects in the ArrayList(Array list may contain duplicate objects)
Difference between static binding and dynamic binding?
What is import java util arraylist?