Java super keyword calling parent class procedure parsing

Time:2021-8-22

This article mainly introduces the Java super keyword calling parent class process parsing. It is introduced in great detail through the example code, which has a certain reference value for everyone’s study or work. Friends in need can refer to it

Don’t say much, just go to the code:

package com.my.pac14;

/**
 * @auther Summerday
 */
public class SuperTest {
  public static void main(String[] args) {
    SubClass sb = new SubClass(20);
    //Create a subclass object and call the print method of the subclass
    sb.print();
  }
}

class BaseClass {
  public int a = 5;
  public int b;
  static public int c = 30;

  BaseClass(int b) {
    this.b = b;
    System.out.println("superb:" + this.b);
  }

  public void print() {
    System. Out. Println ("a of parent class =" + a);
    //System.out.println(this.a);
  }

  public void read() {
    System.out.println("read..");
  }
}

class SubClass extends BaseClass {
  public int a = 10;
  //The instance variables in the subclass will be hidden from those in the parent class, so the following this. B = 0
  public int b;
  static public int c = 40;
  
  SubClass(int b) {
  //Call the constructor of the parent class
    super(b);//20
    System.out.println("superb:" + super.b + ",subb:" + this.b);//0
  }

  public void print() {
    //Print a = 5 in parent class
    System.out.println(super.a);//5
    //Call the print method of the parent class to print a = 5
    super.print();//5
    //Print a directly, a of the current class will be printed, and a = 10 of the subclass will be printed
    System. Out. Println ("a of subclass =" + a);
    //Class variables are also overridden
    System.out.println(c);
    //Use the parent class name to access the class variable of the parent class
    System. Out. Println ("class variable C in parent class =" + baseClass. C);
    //There is no override, and there is no need to use super to call the parent method
    read();
  }

}

Super is used to restrict the object to call instance variables or methods inherited from the parent class. Therefore, like this, super cannot appear in static modified methods( Because the static modified method belongs to a class, the caller will be a class, not an object)

Call parent class member variable

Instance variable

You can directly use super.xx to restrict access to the instance variables of the parent class.

//Parent class
public int a = 5;
//Subclass
public int a = 10;
//Access the instance variable of the parent class in the child class
public void print() {
  //Print a = 5 in parent class
  System.out.println(super.a);//5
  //Print a directly, a of the current class will be printed, and a = 10 of the subclass will be printed
  System. Out. Println ("a of subclass =" + a);
}

Class variable

If the parent class variable is a class variable decorated with static, you need to restrict access to its class variable with the parent class name. XX( Although super can be used to access class variables in the parent class, as we said before, it is extremely nonstandard and is not recommended.)

//Parent class
static public int c = 30;
//Subclass
static public int c = 40;
//Access the class variables of the parent class in the child class
public void print() {
  //Class variables are also hidden
  System.out.println(c);
  //Use the parent class name to access the class variable of the parent class
  System. Out. Println ("class variable C in parent class =" + baseClass. C);
}

Summary:

  • If the subclass does not contain a member variable with the same name as the parent class, there is no need to explicitly call it with super or parent class name.
  • If a member variable is accessed in an insufficient method, but the caller is not explicitly specified, the order in which the member variable is found:
    • Find out if there is in this method.
    • Find if there is in this class.
    • Find whether there is in the parent class.
    • ……
  • If it is not found, an error will be reported.

When creating a subclass object, the system will allocate memory not only for the instance variables defined in the class, but also for all the instance variables inherited from the parent class, even if the subclass defines an instance variable with the same name as the parent class. Therefore, the instance variables defined in the subclass with the same name as the parent class will not completely overwrite the instance variables defined in the parent class, but simply hide them.

Call the method of the parent class

As we said before, the instance method defined in the subclass with the same name as the parent class and the same parameter list (static method cannot be overridden) is the so-called method override. At this time, the subclass’s method will override the parent’s method.

  • If you want to call the instance method of the parent class with the same name in the subclass, you can use: super. Parent class method name;
  • If it is a class method, it can be called with the parent class name. Parent class method name( Super is still OK, but not recommended)
  • If you don’t have the same name, you don’t need to explicitly call the parent method with super.
//Parent class
public void print() {
  System. Out. Println ("a of parent class =" + a);
  //System.out.println(this.a);
}
public void read() {
  System.out.println("read..");
}
//Subclass
 public void print() {
  //Call the print method of the parent class to print a = 5
  super.print();//5
  //There is no override, and there is no need to use super to call the parent method
  read();
}

Call the parent class constructor

In the previous article, a summary has been made about using the super keyword to call the parent class constructor in the subclass. For details, please refer to the analysis of the use process of Java inheritance constructor

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support developpaer.

Recommended Today

SQL exercise 20 – Modeling & Reporting

This blog is used to review and sort out the common topic modeling architecture, analysis oriented architecture and integration topic reports in data warehouse. I have uploaded these reports to GitHub. If you are interested, you can have a lookAddress:https://github.com/nino-laiqiu/TiTanI recorded a relatively complete development process in my hexo blog deployed on GitHub. You can […]