1024programmer Java In Java, when creating a subclass object, will the parent class object be created first? (What if the parent class is an abstract class?)

In Java, when creating a subclass object, will the parent class object be created first? (What if the parent class is an abstract class?)

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);
}

}

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/in-java-when-creating-a-subclass-object-will-the-parent-class-object-be-created-first-what-if-the-parent-class-is-an-abstract-class-3/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索