1024programmer Blog In java, the subclass calls the parent class construction method, precautions_Subclass calls the parent class construction method_PJlei’s Blog

In java, the subclass calls the parent class construction method, precautions_Subclass calls the parent class construction method_PJlei’s Blog

There is a characteristic of inheritance, that is, subclasses cannot access parent classprivatefield or privatemethod. For example, the Student class cannot accessPersonnameandagefield:

class Person {

private String name;
private int age;
}

classStudent extends Person {

public String hello() {

return “Hello, “ + name; // Compile error: cannot accessname Field
}
} span>

This weakens the role of inheritance. In order to allow subclasses to access the fields of the parent class, we need to change private to protected. Fields modified with protected can be accessed by subclasses:

class Person {

protected String name;
protected int age;
}

classStudent extends Person {

public String hello() {

return “Hello, “ + name; // OK!
}
}

Therefore,protected Keywords can control the access rights of fields and methods within the inheritance tree, a protected Fields and methods can be accessed by its subclasses and subclasses of subclasses, which we will explain in detail later.

2. The use of super

superThe keyword indicates the parent class (super class). When a subclass refers to a field of the parent class, you can use super.fieldName. For example:

class Student extends Person {

public String hello() {

return “Hello, “ + super.name;

}
}

The above code execution will cause compilation errors, the student construction method cannot call the Person construction method.

Analysis: The construction method of any class must first construct the construction method of the parent class. If there is no explicit call to the constructor of the parent class, the compiler will automatically add a sentence of super() for us, so the constructor of the Student class is actually like this

The writing method in the Student class is

uploading.4e448015.gifUploading…ReuploadCancel

Summary: If the parent class does not have a default constructor, the subclass must explicitly callsuper() and give parameters to let the compiler locate A suitable constructor. At the same time, the subclass will not inherit any parent class construction method, the subclass default construction method is automatically generated by the compiler, not inherited

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/in-java-the-subclass-calls-the-parent-class-construction-method-precautions_subclass-calls-the-parent-class-construction-method_pjleis-blog/

author: admin

Previous article
Next article

Leave a Reply

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

The latest and most comprehensive programming knowledge, all in 1024programmer.com

© 2023 1024programmer - Encyclopedia of Programming Field
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

首页
微信
电话
搜索