https://blog.csdn.net/banzhengyu/article/details/81039757
In Java, when creating a subclass object, will the parent class object be created first? (What if the parent class is an abstract class?)
Ask a question: If when creating a subclass object, the parent class object will be created first, then if the parent class is an abstract class, then the abstract parent class will also be instantiated. This is different from the abstract class. Can’t instantiate each other!
package javase5;
public abstract class Animal {
int age;
public Animal() {
this.age = 6;
System.out.println("I am the parameterless constructor of the Animal class");
}
}
package javase5;
public class Cat extends Animal{
public Cat() {
// super();
//Even if you don’t write the super(); line of code, the system will automatically call the parent class’s no-argument constructor by default.
System.out.println("I am the parameterless constructor of Cat class");
}
public static void main(String[] args) {
Cat cat = new Cat();
System.out.println(cat.age);
Cat cat2 = new Cat();
System.out.println(cat2.age);
}
}