[java notes] class inheritance

catalogue

Characteristics of inheritance relationship:

Three variables have the same name:

The characteristics of calling member methods in inheritance:

override of methods in inheritance:

Preserve the functionality of the method of the parent class when overridden

Access characteristics of construction methods in inheritance:

super keyword usage:

tihs keyword:

Memory diagram of super/this:

Object oriented features: encapsulation, inheritance, polymorphism

Inheritance is the premise of polymorphism. If there is no inheritance, there will be no polymorphism

Parent class (base class, superclass) - subclass (derived class)

Characteristics of inheritance relationship:

1. A subclass can have the contents of the parent class

2. Subclasses can also have their own proprietary content

In the inheritance relationship, "a subclass is a parent", and a subclass can be treated as a parent

Define the format of the parent class (an ordinary class definition)

public class parent class name{

           //...}

public class # subclass name # extends parent class name{

      //..  

}

In the inheritance relationship between parent and child classes, if the member variables have the same name, there are two ways to access when creating child class objects:

Access member variables directly through subclass objects:

Whoever is on the left of the equal sign has priority. If not, look up

Three variables have the same name:

Local variables: write directly

Member variable of this class: this variable

Member variable of parent class: super variable

public class fu {
    int num=30;
}

public class zi extends fu{
    int num=20;
    public void method() {
        int num = 10;
        System.out.println(num);//10
        System.out.println(this.num);//20
        System.out.println(super.num);//30
    }

}

The characteristics of calling member methods in inheritance:

When the child class method and parent class method do not have the same name: call directly

public class fu {
 public void methodfu(){
     System.out.println("Parent method execution");
 }
}
public class zi extends fu{
     public void methodzi(){
         System.out.println("Sub method execution");
     }
}
zi z=new zi();

z.methodzi();Sub method execution
z.methodfu();Parent method execution

When the child class method and parent class method have the same name:

Whoever creates the object will be used first. If not, look up

public class fu {
 public void method()
    {
        System.out.println("Parent method execution");
    }
}
public class zi extends fu{
     public void method(){
         System.out.println("Sub method execution");
     }
}

zi z=new zi();
z.method();//Sub method execution
fu f=new fu();
f.method();//Parent method execution

matters needing attention:

Whether it's a member method or a member variable, if it doesn't, it's looking up for the parent class and never looking down for the child class

override of methods in inheritance:

Rewriting concept: in the inheritance relationship, the name of the method is the same, and the parameter list is the same

override: the method name is the same, and the parameter list is overwritten

overload: same method name, different parameter list

characteristic:

If subclass objects are created, subclass methods are preferred

Precautions for method override:

1. You must ensure that the names of methods and parameter lists between parent and child classes are the same

@Override: it is written in front of the method to detect whether it is an effective override

Even if this annotation is not written, as long as it meets the requirements, it is also the correct method to overwrite it

2. The return value of the subclass method must be less than or equal to the return value range of the parent method

Small expansion: Java Lang.Object class is the highest parent class (ancestral class) of all classes, Java Lang.string is a subclass of object

public class fu {
 public Object method()
    {
        System.out.println("Parent method execution");
        return null;
    }
}
public class zi extends fu{
     public String method(){
         System.out.println("Sub method execution");
         return null;
     }
}The return value type of the subclass method is less than or equal to that of the parent class

3. Permission of the subclass method is greater than or equal to the permission modifier of the parent method

Tips for small expansion: public > protected > (default) > private

Note: (default) it is not the keyword default, but nothing is written and left blank

Preserve the functionality of the method of the parent class when overridden

When overriding, add super to the methods of subclasses Parent class method (with the same name)// You can preserve the functionality of the parent class

Example: subclass rewrites the show function (adding functions to the original):

public class fu {
  public void show(){
      System.out.println("Display number");
  }
}
public class zi extends fu{
  public void show(){
      super.show();
      System.out.println("Display name");
  }
}
zi z=new zi();
zi.show();Display number display name

Access characteristics of construction methods in inheritance:

1. There is a default implicit "super()" call in the subclass construction method, so the parent class construction must be called first, and then the subclass construction must be executed

public class fu {
  public fu(){
      System.out.println("Parent construction");
  }
}
public class zi extends fu{
  public zi(){
      System.out.println("Substructure");
  }
}  
zi z=new zi();Parent construct and child construct

2. You can use the super keyword to call the parent class overload construction for the subclass construction

public class fu {
  public fu(int num){
      System.out.println("Parent construction");
  }
}
public class zi extends fu{
  public zi(){
      super(10);//Call the parent class overload constructor
      System.out.println("Sub execution");
  }
}

If the constructor of the parent class has been customized, the default construction parameters cannot be automatically called in the subclass (there are no default construction parameters), and super (parameter class table) is used to realize the construction of the parent class

3. The parent class construction call of super must be the first statement of the subclass construction method. A super construct cannot be called more than once for a subclass construct

Summary:

The subclass must call the constructor of the parent class. If it is not written, it will be given super();

If it is written, it will be called with the specified super. There can only be one super, and it must be the first.

super keyword usage:

1. In the member method of the subclass, access the member variable of the parent class (before reference)

2. In the member method of the subclass, access the member method of the parent class (refer to the previous)

3, in the subclass member method, call the parent class construction method (before reference).

tihs keyword:

1. In the member method of this class, access the member variables of this class

2. In the member method of this class, access another member method of this class

? 3. In the constructor of this class, access another constructor of this class

be careful:

1. The call to this () must also be the first and only statement of the construction method

2. super and this construction calls cannot be used at the same time

Memory diagram of super/this:

Three characteristics of java inheritance:

1. The direct parent class of a class can only have one (prevent different parent classes from having the same member method, which can be inherited with diamond in c + +)

2. java language can have multi-level inheritance

The highest class in inheritance: object class

3. The immediate parent of a subclass is unique, but a parent can have many subclasses

(inheritance is equivalent to a tree)

Keywords: Java Singleton pattern

Added by avatar.alex on Tue, 15 Feb 2022 15:01:00 +0200