Java -- object oriented (medium) -- super keyword and method rewriting

catalogue

1, Basic grammar

2, Benefits / details of super to programming

3, Comparison between super and this

super class explanation code

4, Method override / override

1. Basic introduction

2. Precautions and use details

  • Basic introduction

super represents the reference of the parent class and is used to access the properties, constructors and methods of the parent class.

1, Basic grammar

        1. The property of the parent class of. private cannot be accessed Attribute name;

        2. Access the method of the parent class, but cannot access the private method super of the parent class Method name (parameter list);

        3. Access the constructor # super (parameter list) of the parent class; It can only be placed in the first sentence of the constructor and only one sentence can appear

2, Benefits / details of super to programming

        1. Benefits of calling the parent class constructor (clear division of labor, parent class properties are initialized by the parent class, and child class properties are initialized by the child class)

        2. When a child class has the same name as a member (property and method) in the parent class, in order to access the member of the parent class, you must pass super. If there is no duplicate name, using super, this and direct access will have the same effect

        3.super's access is not limited to the direct parent class. If there are members with the same name in the grandfather class and this class, you can also use super to access the members of the grandfather class; If there are members with the same name in multiple base classes (superior classes), the principle of proximity shall be followed when using super access.

3, Comparison between super and this

Distinguishing pointsthissuper
Access properties

Access the properties in this class. If this class does not

This property continues to find from the parent class

Find properties from parent class
Call method

Access methods in this class, if this class does not

This method continues to find from the parent class

Find method from parent class
Invoking Constructors

Calling this class constructor must be placed on the first line of the constructor

Call the parent class constructor, which must be placed in

First line of subclass constructor

specialRepresents the current object

Accessing parent objects in subclasses

(reference of parent class)

super class explanation code

public class super01 {
    public static void main(String[] args) {
        B b = new B();//Subclass object
        //b.sum();
        b.Test();
    }
}


public class Base { //Parent class of parent class

    public int n1 = 999;
    public int age = 20;

    public void cal(){
        System.out.println("Base Class cal method....");
    }
    public void eat(){
        System.out.println("Base Class eat method....");
    }
}


public class A extends Base{ //Parent class
    //attribute
    public int n1 = 100;
    protected int n2 = 200;
    int n3 = 300;
    private int n4 = 400;
    //constructor 
    public A(){}
    public A(String name){}
    public A(String name, int age){}
    //method
    public void test100(){}
    protected void test200(){}
    void test300(){}
    private void test400(){}

    //Detailed supplementary code
    public void cal(){
        System.out.println("A Class cal method....");
    }
}


public class B extends A{ //Subclass
    //Access the property of the parent class, but cannot access the private property super of the parent class Attribute name;
    public void hi(){
        System.out.println(super.n1 + " " + super.n2 + " " + super.n3);
    }
    //Access the method of the parent class, but cannot access the private method super of the parent class Method name (formal parameter list);
    public void ok(){
        super.test100();
        super.test200();
        super.test300();
    }
    //Access the constructor super (parameter list) of the parent class; It can only be placed in the first sentence of the constructor, and only one sentence can appear
    public B(){
        //super();
        super("jack",10);
    }

    //Detailed supplementary code
    public int n1 = 888;
    public void cal(){
        System.out.println("B Class cal method...");
    }
    public void sum(){
        System.out.println("B Class sum method....");
        //You want to call the cal method of parent class A
        //Because subclass B has no cal method, the following three methods can be used

        //When finding cal method (cal() and this Cal ()), in the following order
//         1. Find this class first. If so, call
//         2. If this class does not exist, find the parent class (if it exists and can be called, call)
//         3. If there is no parent class, continue to find the parent class of the parent class until the Object class is found
//         Tip: if the method is found but cannot be accessed, an error will be reported
//              If the method is not found in the process of searching, it will prompt that the method does not exist
        //cal();
        //this.cal();// Equivalent to cal ();

        //The order of finding cal method (super.cal()) is to directly find the parent class, which is the same as other rules
        super.cal();

        //The rules for accessing properties are consistent with the rules for finding methods
        System.out.println(n1);
        System.out.println(this.n1);
        System.out.println(super.n1);
    }
    //Write Test method
    public void Test(){
        //super's access is not limited to the direct parent class. If there are members with the same name in the grandfather class and this class,
        //You can also use super to access members of the grandfather class; If multiple base classes (parent classes) have members with the same name,
        // Using super access follows the proximity principle. A - > b - > C, of course, we should also abide by the relevant rules of access rights
        System.out.println("super.n1=" + super.n1);
        super.cal();
    }
}

4, Method override / override

1. Basic introduction

Method rewriting (overriding) means that a method of a subclass has the same name, return type and parameters as a method of the parent class, so we say that the method of the subclass overrides the method of the parent class

2. Precautions and use details

① for the formal parameter list of the subclass method, the method name should be exactly the same as the formal parameter list and method name of the parent class

② 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. For example, the return type of the parent class is Object, and the return type of the child class is String

③ the subclass method cannot reduce the access permission of the parent method (it can be equal or expanded)

public → protected → default → private

Keywords: Java

Added by kovudalion on Sat, 12 Feb 2022 14:26:54 +0200