This is the 10th day of my participation in Gwen Challenge
JAVA object oriented super keyword
1. Definition:
In JAVA classes, we use super to refer to the component of the superclass, and this to refer to the current object. If a class inherits from another class, when we new the instance object of that subclass, the subclass object will have a superclass object in it. How to refer to the parent object inside? This refers to the reference to the current object, and super refers to the parent object inside the current object.
2. Use:
The super keyword is similar to this in that it has three applications:
- Call the parent class to call the property
- Call the parent class callable method
- Call the parent class corresponding constructor
Application 1: Call the parent class attribute:
The super keyword is usually used to contain inheritance relationships. Let’s use examples to demonstrate how to invoke the super keyword. For example, there is a parent class Person, which contains attributes (ID,name,sex), and a subclass Student, which inherits from the Person class. And has its own unique attribute score, so how do we call the common attribute defined by the parent class in a subclass? The code is as follows:
The parent class:
package cn.hz;
/ * * *@author hz
* @version1.0 * Parent class */
public class Person {
public Integer id; // Attribute: student id
public String name; // Attribute: student name
public String sex; // Attribute: student gender
}
Copy the code
The subclass:
package cn.hz;
/ * * *@author hz
* @version1.0 * /
public class Student extends Person {
public Integer studentScore; // Attribute: student score
public void show(a){
System.out.println("Student information is as follows: Name:"+super.name+", gender:+
super.sex+"No. :"+super.id+", score:"+this.studentScore); }}Copy the code
To call a parent class in a subclass to define a property, the syntax is as follows:
superAttributes.Copy the code
Pay special attention to, the subclass when calling the superclass attribute, must pay attention to attribute access modifier (inheritance specific use to meet the previous section access modifiers in the table), not all attributes of the parent class subclasses can through super calls, such as the parent class encapsulation, attribute use private complete encapsulation, on this occasion we will not be able to directly call the parent class attribute, How do you do that? You need to call a method of the parent class to operate.
Application 2: Call the parent method:
If a subclass inherits its parent class, it can use the attributes and methods of its parent class. We saw how a subclass calls the attributes of its parent class.
The parent class:
package cn.hz;
/ * * *@author hz
* @version1.0 * Parent class: encapsulation */
public class Person {
private Integer id; // Attribute: student id
private String name; // Attribute: student name
private String sex; // Attribute: student gender
public Integer getId(a) {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName(a) {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex(a) {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public void test(a){
System.out.println("Superclass method"); }}Copy the code
The subclass:
package cn.hz;
/ * * *@author hz
* @version1.0 * /
public class Student extends Person {
public Integer studentScore; // Attribute: student score
public void show(a){
System.out.println("Student information is as follows: Name:"+super.getName()+", gender:+
super.getSex()+"No. :"+super.getId()+", score:"+this.studentScore);
}
public void test2(a){
System.out.println("Call the parent method");
super.test(); }}Copy the code
A child class cannot call a property of the parent class. However, a child class can get the value of the parent class by calling the public set method provided for the property in the parent class. There is also a normal method defined in the parent class, which can also be called by the keyword super.
Note: in this case, because the subclass has inherited the attributes and methods of the parent class, it is possible to omit super or use this instead of calling the method of the parent class (in actual development, it is usually directly omitted). The code is as follows:
Super keyword omission:
package cn.hz;
/ * * *@author hz
* @version1.0 * /
public class Student extends Person {
public Integer studentScore; // Attribute: student score
public void show(a){
System.out.println("Student information is as follows: Name:"+getName()+", gender:+
getSex()+"No. :"+getId()+", score:"+this.studentScore);
}
public void test2(a){
System.out.println("Call the parent method"); test(); }}Copy the code
Use this keyword:
package cn.hz;
/ * * *@author hz
* @version1.0 * /
public class Student extends Person {
public Integer studentScore; // Attribute: student score
public void show(a){
System.out.println("Student information is as follows: Name:"+this.getName()+", gender:+
this.getSex()+"No. :"+this.getId()+", score:"+this.studentScore);
}
public void test2(a){
System.out.println("Call the parent method");
this.test(); }}Copy the code
Application 3: Call the parent constructor:
A subclass needs to use the constructor when creating an object, and it must use the constructor of its parent class.
The parent class:
package cn.hz;
/ * * *@author hz
* @version1.0 * Parent class: encapsulation */
public class Person {
private Integer id; // Attribute: student id
private String name; // Attribute: student name
private String sex; // Attribute: student gender
/ / set/get omitted
public Person(a) {}public Person(String name) {
this.name = name;
}
public Person(Integer id, String name) {
this.id = id;
this.name = name;
}
public Person(Integer id, String name, String sex) {
this.id = id;
this.name = name;
this.sex = sex; }}Copy the code
The subclass:
package cn.hz;
/ * * *@author hz
* @version1.0 * /
public class Student extends Person {
public Integer studentScore; // Attribute: student score
public Student(Integer studentScore) {
this.studentScore = studentScore;
}
public Student(String name, Integer studentScore) {
super(name);
this.studentScore = studentScore;
}
public Student(Integer id, String name, Integer studentScore) {
super(id, name);
this.studentScore = studentScore;
}
public Student(Integer id, String name, String sex, Integer studentScore) {
super(id, name, sex);
this.studentScore = studentScore; }}Copy the code
We can call the constructor of the parent class using the above code:
super(property);Copy the code
Note that the code must be on the first line of the constructor when a child class calls its parent class’s constructor, just as it calls each other’s constructor
3. Case Study:
Case 1: What happens when the following code runs?
package cn.hz;
/ * * *@author hz
* @version1.0 * Parent class: encapsulation */
public class Person {
private Integer id; // Attribute: student id
private String name; // Attribute: student name
private String sex; // Attribute: student gender
/ / set/get omitted
public Person(a) {
System.out.println("Parent class method without arguments");
}
public Person(String name) {
this.name = name;
System.out.println("Parent name property constructor");
}
public Person(Integer id, String name) {
this(a);this.id = id;
this.name = name;
System.out.println("Parent id,name property constructor"); }}Copy the code
package cn.hz;
/ * * *@author hz
* @version1.0 * /
public class Student extends Person {
public Integer studentScore; // Attribute: student score
public Student(a) {
System.out.println("Subclass parameterless constructor");
}
public Student(String name, Integer studentScore) {
super(name);
this.studentScore = studentScore;
System.out.println("Class attribute name constructor");
}
public Student(Integer id, String name, Integer studentScore) {
super(id, name);
this.studentScore = studentScore;
System.out.println("Subclass attribute name, ID constructor"); }}Copy the code
package cn.hz;
/ * * *@author hz
* @version1.0 * /
public class StudentTest {
public static void main(String[] args) {
// Create a student object
Student student1=new Student();
// Create a student object
Student student2=new Student("zs".80);
// Create a student object
Student student3=new Student(10001."ls".90); }}Copy the code
Just saw this case, may do not know where do many students feel, this case is mainly to investigate people grasp of the object creation process and super and this keyword proficiency, first we analyze student1 object creation, subclass contains no arguments constructor, although not explicitly call subclass a no-parameter constructor of the parent class constructor, Student1 creates a subclass that calls its parent class constructor. Student1 creates a subclass that calls its parent class constructor.
If the first line contains super(name), the constructor of the parent class is called. If the parent class contains the constructor, the constructor is directly executed.
Student3: call this() on the first line if super(id, name) is on the first line. This () means that the parent class calls its own constructor with no arguments, so the whole procedure call executes three constructors, and the final result is as follows:
Summary:
- Subclasses call their parent class constructors in the same way that their own constructors call each other, and this code must be on the first line of the constructor;
- If the subclass constructor is explicitly called, the subclass constructor is called when the object is created.
- If there is no explicit call to the parent constructor in a subclass constructor, the parent’s no-argument constructor is called by default, and the parent constructor must be called when the subclass creates an object.