Yes , The constructor of an abstract class can be called. why not? Use super() from subclass to call the super class argument constructor(s). If you dont use super(), then by default the no-arg constructor of super class will be called.
If you call
Dog dog = new Dog();
By default the super class default constructor[no-arg constructor] will be called.
If you call
Dog dog = new Dog("doggie1");
Unless you explicitly call super() inside the one argument constructor of Dog
, the default super class constructor will not be called.
Try it..
Edit: If you don't call super() inside the one argument constructor of Dog
, then also the default super class constructor will be called.
If you want to explicitly call the argument constructor , say one argument constructor of the super class, you have to explicitly make the call super("value");
Animal() {
System.out.println("Animal superconstructor");
}
Edit 02:
Sample program and output
(1)
public class SubClass extends SuperClass {
SubClass(String str) {
super(str);
}
SubClass() {
}
public static void main(String[] args) {
new SubClass("hello");
}
}
abstract class SuperClass {
SuperClass() {
System.out.println("I am SuperClass()");
}
SuperClass(String str) {
System.out.println("I am SuperClass(String str)");
}
}
//output: I am SuperClass(String str)
(2)
public class SubClass extends SuperClass {
SubClass(String str) {
}
SubClass() {
}
public static void main(String[] args) {
new SubClass("hello");
}
}
abstract class SuperClass {
SuperClass() {
System.out.println("I am SuperClass()");
}
SuperClass(String str) {
System.out.println("I am SuperClass(String str)");
}
}
//output: I am SuperClass()
(3)
public class SubClass extends SuperClass {
SubClass(String str) {
}
SubClass() {
}
public static void main(String[] args) {
new SubClass();
}
}
abstract class SuperClass {
SuperClass() {
System.out.println("I am SuperClass()");
}
SuperClass(String str) {
System.out.println("I am SuperClass(String str)");
}
}
// output: I am SuperClass()