1, super keyword
super represents the reference of the parent class and is used to access the properties, methods and constructors of the parent class.
1.1 basic grammar
Access the properties of the parent class
Cannot access the property of the private modifier in the parent class
super.Attribute name;
Method to access the parent class
Cannot access private decorated methods in a parent class
super.Method name(parameter list);
Constructor to access the parent class
super(parameter list);
It can only be placed in the first sentence of the constructor, and only one sentence can appear
1.2 super depth
Each time a subclass object is created, the parent class space is initialized first, and then the subclass object itself is created. The purpose is that if the subclass object contains its corresponding parent class space, it can contain the members of its parent class. If the parent class member is not private decorated, the subclass can use the parent class member at will.
The benefits of calling the parent class constructor: the division of labor is clear. The parent class attribute is initialized by the parent class and the child class attribute is initialized by the child class
When members (properties and methods) in a subclass have the same name as members in the parent class, super must be used in order to access members of the parent class.
If there is no duplicate name, the effects of using super, this and direct access are the same.
If you do not use the super keyword to call a method, the order of finding the method is: if this class has and can be called, then call the method of this class. If it does not or cannot be called, it will find the parent class until it can be called.
If you use the super keyword to call a method, you will find the parent class in turn. If you can't find or call it, you will report an error. Until it is found, the original method will not be called.
Super access is not limited to the parent class. You can use super to access the members of the parent class of the parent class. If you have the same name, follow the principle of proximity.
class Animal { public void eat() { System.out.println("animal : eat"); } } class Cat extends Animal { public void eat() { System.out.println("cat : eat"); } public void eatTest() { this.eat(); // This calls the method of this class super.eat(); // super calls the method of the parent class } } public class ExtendsDemo08 { public static void main(String[] args) { Animal a = new Animal(); a.eat(); Cat c = new Cat(); c.eatTest(); } } The output result is: animal : eat cat : eat animal : eat
1.3 difference between super and this
Distinguishing points | this | super |
---|---|---|
Access properties | Access the attribute in this class. If there is no such attribute in this class, continue to search from the parent class until it is found. Otherwise, the compilation fails | Access the properties of the parent class and directly search from the parent class until it is found, otherwise the compilation fails |
Call method | Access the method in this class. If there is no such method in this class, continue to search from the parent class until it is found. Otherwise, the compilation fails | Directly access the method in the parent class until it is found, otherwise the compilation fails |
Invoking Constructors | Calling the constructor of this class must be placed on the first line of the constructor | Calling the parent class constructor must be placed on the first line of the child class constructor |
special | Represents the current object | Accessing parent objects in subclasses |
2, Method rewrite
If a member method with the same name appears in the parent class of a child class, the access is a special case, which is called method override.
Override: as like as two peas in the subclass, the same method is applied to the parent class (the return value type, the method name and the parameter list are the same), and the coverage effect will also appear. Also known as rewriting or copying.
class Fu { public void show() { System.out.println("Fu show"); } } class Zi extends Fu { //The subclass overrides the show method of the parent class public void show() { System.out.println("Zi show"); } } public class ExtendsDemo05{ public static void main(String[] args) { Zi z = new Zi(); // There is a show method in the subclass. Only the rewritten show method is executed z.show(); // Zi show } }
If the subclass method overrides the parent method, the permission must be greater than or equal to the parent permission. As like as two peas, the subclass method covers the parent class method. The return value, function name and parameter list are exactly the same.
Return value type of method override [key]
The return type of the subclass method should be the same as that of the parent method, or it is a subclass of the parent return type
If the return type of a subclass is not the return type of the parent class or a subclass of the return type of the parent class, but the method name and parameter list are the same, a compilation error occurs
For example, the hi method constitutes an override, and String is a subclass of Object
public class A { public Object hi() {} } class B extends A { public String hi() {} }
Method override
Subclass methods cannot reduce the access rights of parent methods
For example, if the permission modifier of a method of a parent class is public, the permission modifier of a method overridden by a child class can only be public
public class A { Object hi() {} } class B extends A { // The permission can only be greater than that of the hi method in class A, but not less public String hi() {} }
The difference between rewriting and overloading
name | Occurrence range | Method name | parameter list | Return type | Modifier |
---|---|---|---|---|---|
Overload | This category | Must be the same | There is at least one difference in type, number, or order | No requirement | No requirement |
Override | Parent child class | Must be the same | identical | For a method overridden by a subclass, the return type must be the same as Frey, or it must be a subclass of the return type of the parent method | The permission modifier of the method overridden by the subclass must be greater than or equal to the permission modifier of the parent class, and the access scope cannot be narrowed |