13. Class relationship -- inheritance relationship
1. Inheritance relationship
1.1 concept of inheritance
·Inheritance mechanism is an indispensable key concept in object-oriented programming, the foundation of code reusability, and the main way to improve the expansibility and maintainability of software system.
·The so-called inheritance means that the definition of a class can be based on another existing class, that is, the subclass is based on the parent class, so as to realize the reuse of the code of the parent class. The subclass can absorb the properties and behaviors of the existing class, and can expand new capabilities.
Form: [access modifier] [modifier] subclass name extends parent class name {subclass body}
For example:
//Classes: drawings public class Shape { private int x; private int y; public Shape() { } // Method: Drawing public void draw() { } public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } } //Class: circle public class Circle extends Shape { private int r; public Circle() { } // Method: get the diameter public int getDiameter() { return 2 * r; } public int getR() { return r; } public void setR(int r) { this.r = r; } }
We observe the above code and find that the definition of the class Circle inherits the class Shape by using the extends keyword. At this time, we call the Shape the parent class of the Circle, and vice versa. In addition, the radius attribute r is added to the subclass Circle, and the method getDiameter() to get the diameter is added.
1.2 function of inheritance
The main function of inheritance is code reuse. The code shows that the subclass object can directly call the properties and methods of the parent class.
//Class: use scene public class Client { public static void main(String[] args) { Circle circle = new Circle(); // Access the properties and methods of the parent class circle.setX(1); circle.setY(1); circle.draw(); // Accessing properties and methods of subclasses circle.setR(9); circle.getDiameter(); } }
1.3 construction method and inheritance
The subclass constructor always calls the parent constructor first.
By default, the parent class is called to construct a method without parameters.
You can use the super keyword to call any constructor of the parent class on the first line of the subclass constructor.
1.3.1 call of construction method in inheritance
The subclass constructor always calls the parent constructor first.
By default, the parent class is called to construct a method without parameters.
You can use the super keyword to call any constructor of the parent class on the first line of the subclass constructor.
//Classes: drawings public class Shape { public int x; public int y; public Shape() { System.out.println("This is the nonparametric construction method of the parent class!"); } public Shape(int x, int y) { this.x = x; this.y = y; System.out.println("This is the parameter construction method of the parent class!"); } public void draw() { } public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } } //Class: circle public class Circle extends Shape { private int r; public Circle() { // super(); // super(0, 0); System.out.println("This is the nonparametric construction method of subclass!"); } // Method: get the diameter public int getDiameter() { return 2 * r; } public int getR() { return r; } public void setR(int r) { this.r = r; } } //test public class Client { public static void main(String[] args) { Circle circle = new Circle(); } } //output //This is the nonparametric construction method of the parent class! //This is the nonparametric construction method of subclass!
1.3.2 reason for subclass calling parent class construction method
In the Java language, it is required that the subclass has the responsibility to ensure that its inherited parent class enters a stable and complete state as soon as possible. Without this constraint, a method inherited from the parent class of a subclass may use some variables in the parent class, which are not initialized, resulting in some unpredictable consequences.
1.3.3 implementation details of subclass construction method
The statement that calls the parent constructor must be placed in the first statement of the child constructor. If the first statement does not call the construction method of the parent class, the system will automatically insert a statement that calls the default construction method of the parent class, super(); because the default construction method does not take parameters, if the construction method with parameters is defined in the parent class, and the construction method without parameters is not defined, compilation errors will occur. This is the reason why we should define a construction method without parameters when defining the construction method with parameters. For those construction methods with parameters in the parent class, the child class will not call them automatically, and the statement calling them must be written into the construction method of the child class manually.
2. this keyword
This keyword has nothing to do with inheritance. The reason why we put it here to learn is that this and super are very similar. Both have two functions: calling properties and methods, and calling construction methods. The difference is that this is called by this class, and super is called by the parent class.
2.1 purpose of this keyword
2.1.1 this keyword represents the class itself
Use this keyword to reference other construction methods within its own construction method. In the construction method of a class, you can also use this keyword to refer to other construction methods, which can reduce code duplication and keep all construction methods unified, so as to facilitate code modification and maintenance in the future and code reading.
For example:
public class Demo { private int a; private int b; public Demo() { // this reference construction method this(1, 2); } public Demo(int a, int b) { super(); this.a = a; this.b = b; } public int getA() { return a; } public void setA(int a) { this.a = a; } public int getB() { return b; } public void setB(int b) { this.b = b; } }
2.1.2 this keyword represents the object of its own class
-Use this directly
-Use this keyword to reference member variables
-Use this keyword to reference member methods
Note: this keyword must be placed in a non static method
This represents its own object in a class. You can also use this to represent its own object. In other words, there is an implicit member variable in each class. The type of the member variable is the type of the class, and the name of the member variable is this
public class Demo { private Demo instance; public Demo() { instance = this; } }
This refers to a member variable within a class's method or construction method. You can use the format "this. Member variable name" to refer to a member variable name, which is often used to distinguish a member variable with the same name from a local variable.
public class Demo { private int a; private int b; public Demo() { } public int getA() { return a; } public void setA(int a) { // this reference member variable this.a = a; } public int getB() { return b; } public void setB(int b) { // this reference member variable this.b = b; } }
This refers to member methods within a class. When member methods call each other, "this. Method name (parameter)" can also be used for reference.
public class Demo { public Demo() { } public void method1() { //this reference member method this.method2(); } public void method2() { } }
3. super keyword
The use of super keyword is similar to this, but super must be used in the inheritance relationship, referring to the parent class or the object of the parent class.
3.1 super keyword usage
3.1.1 super represents the parent class
To call the construction method of the parent class in the subclass construction method, it should be noted that the super statement can only appear in the first line of the subclass construction method body.
3.1.2 super calls parent class member variable
When the local variable in the subclass method body or the member variable of the subclass has the same name as the parent member variable, that is, when the local variable of the subclass covers the parent member variable, the "super. Member variable name" is used to refer to the parent member variable
3.1.3 super calls member methods of the parent class
When the member methods of the subclass cover the member methods of the parent class, that is, the subclass and the parent class have exactly the same method definitions (the method body can be different). In this case, use the "super. Method name (parameter list)" method to access the methods of the parent class.
3.2 difference between super and this
this usually refers to the current object, and super usually refers to the superclass.
4. Method coverage
The function of inheritance is reuse, that is, the subclass directly uses the properties and methods of the parent class.
However, sometimes, the subclass wants to modify the method body of the parent class's method. What can we do?
The first approach is to create a new method with a different name for the subclass to implement the new logic. However, this approach will result in that the subclass still contains the method in the parent class, but it should not be used to break the encapsulation. The second is that we want the method in the subclass to be the same as the declaration form of the parent method, but the specific method body is different, which is called method coverage.
Suggestion: use an example extension from the beginning to the end of the abstract chapter, all the way to the abstract part.
4.1 definition and principle of method coverage
4.1.1 definition of method coverage
Subclass can override a method in the parent class, which is called method override, also called method override. It is a very important knowledge point in inheritance. If the subclass needs to modify the method body of the method inherited from the parent class, method override can be used.
4.1.2 principle of method coverage
Access to the same name, the same parameter and the same return value cannot be reduced
Method coverage example:
//Superclass
public class Animal { String name; Integer age; public Animal() { } public Animal(String name, Integer age) { this.name = name; this.age = age; } void run() { System.out.println("One" + this.age + "Year old" + this.name + "Running!~~"); } void sound() { System.out.println("One" + this.age + "Year old" + this.name + "Yelling!~~"); } }
//Subclass:
public class Tiger extends Animal { String color; public Tiger(String name, Integer age, String color) { super(name, age); this.color = color; } void hunt() { System.out.println("One" + this.color + "Of" + this.name + "Hunting!~~"); } void sound() { System.out.print("I am a tiger~"); super.sound(); } }
It can be mentioned here that the tiger sound() method is used instead of method override, and sound() will be inherited by Tiger class, which violates the encapsulation feature of object-oriented. Object encapsulates properties and methods that can be used by the object. Therefore, although the above method implements the new algorithm, it has serious loopholes and destroys the encapsulation of objects. If a subclass needs to modify a new method inherited from its parent, you can use method override to do so. In a subclass, if you declare a method with the same name, parameter, return value type and access permission as the parent, you can overwrite the method in the parent class. The subclass object calls this method, which will automatically bind to the new method after subclass coverage.
Sample code - test class
public class Tester { public static void main(String[] args) { Tiger t = new Tiger("Northeast Tiger", 1, "white"); t.sound(); } }