JAVA - inheritance and method rewriting

inherit

Definition of Inheritance

There are relationships between classes and classes, and this relationship is called an association. For example: a department store class and a salesperson class are an association, and a student class and a teacher class are also an association. There are many relationships between two classes (dependency, composition, etc.), and inheritance is just one of them.

  • The essence of inheritance is the abstraction of a certain class , so as to achieve better modeling of the real world.

  • The two classes of the inheritance relationship, one is the subclass (derived class), and the other is the parent class (base class, super class). The subclass inherits the parent class, and the keyword extends is used to indicate
    that extends means "extension", and the subclass is the extension of the parent class

The parent class is (all subclasses) commonality extraction

public class Zi extends Fu{
    
    
    //内容
}

inherited attention

  1. There is only single inheritance between classes and no multiple inheritance! ! !

A subclass can only "directly" inherit from one parent class, just like a person has only one biological father,
and a parent class can have multiple subclasses, just like a father can have multiple children

  1. The properties and methods in the parent class can be inherited by subclasses, and subclasses can also have their own exclusive content
  2. In the parent-subclass inheritance relationship, if the member variable has the same name , there are two ways to access it when creating a subclass object:
    a. Directly access the member variable through the subclass object:
    whoever is on the left of the equal sign will be used first, and if there is no Then look up.
    b. Indirectly (in the method) access member variables through member methods:
    whoever the method belongs to , whoever is the first to use, if not, look up.
  3. In the inheritance relationship between parent and child classes, when creating a subclass object with the same name as the parent class, whoever owns it. When there is no subclass, it will look up to the parent class and never look down to the subclass .

super keyword

After the subclass inherits the parent class, you can use this in the subclass to indicate access or call the properties or methods in the subclass, and use super to indicate access or call the properties and methods in the parent class.

use of super

  1. Access properties in the parent class
public class Fu {
    
    
    String name="xxx";
}
public class Zi extends Fu{
    
    
    String name ="yyy";
    public void showname()
    {
    
    
        System.out.println(name);
        System.out.println(this.name);
        System.out.println(super.name);
    }
}
  1. Access methods in the parent class
public class Fu {
    
    
    public void show()
    {
    
    
        System.out.println("唱唱歌");
    }
}
public class Zi extends Fu{
    
    
    public void show()
    {
    
    
        System.out.println("跳跳舞");
    }
    public void show_together()
    {
    
    
        show();
        this.show();
        super.show();
    }
}
  1. Call the constructor in the parent class
public class Fu {
    
     
}
public class Zi extends Fu{
    
    
    //编译通过,编译器会赠送一个无参构造
    //super();
    public Zi(){
    
    
    }
}
//一定是先调用父类构造,后执行子类构造

Precautions

  1. Using super to call the parent class constructor must be the first statement in the constructor.
  2. super can only appear in subclass methods or constructors.
  3. super and this cannot call the constructor at the same time. (because this is also the first statement in the constructor)

The difference between super and this

  1. represent things are different

this: represents the caller object of the method to which it belongs
super: represents the application space of the parent class object

  1. The premise of use is different

this: can also be used under the premise of non-inheritance
super: can only be used under the condition of inheritance

  1. call constructor

this: calls the constructor of this class
super: calls the constructor of the parent class

method override

override

Why rewrite?

The subclass inherits the parent class and inherits the methods in the parent class, but the methods in the parent class may not necessarily meet the functional needs of the subclass, so the method needs to be rewritten in the subclass.

  1. In the inheritance relationship, the method name is the same, and the parameter list is also the same . Also called: covering, overwriting
  2. Method rewriting only exists between subclasses and parent classes (including direct parent classes and indirect parent classes). Methods in the same class can only be overloaded, not overridden .
  3. Static methods cannot be overridden
    a. The static method of the parent class cannot be rewritten as a non-static method by the subclass, and there will be an error in compilation b. The
    non-static method of the parent class cannot be rewritten as a static method by the subclass, and an error will occur in compilation
    c. The subclass can define the same as the parent class A static method with the same name as a static method (but this is not coverage.
    Private methods cannot be overridden by subclasses. After subclasses inherit the parent class, they cannot directly access the private methods in the parent class, let alone rewriting.
//编译通过
//只是两个类自己的私有方法
public class Fu {
    
    
    private void show() {
    
    
    }
}
public class Zi extends Fu{
    
    
	private void show() {
    
    
    }
}

overridden syntax

  1. method names must be the same
  2. The argument lists must be identical
  3. Access control modifiers can be expanded, but not narrowed

public > protected > default > private
scope:
public----anywhere can
protect----same package/subclass (subclasses can be under different packages)
default-same package
private-----same class

Note: In general, the rewritten method will be completely consistent with the declaration of the method in the parent class, only the implementation of the method is different. (that is, the codes in the braces are different)

  1. @Override: Written in front of the rewriting code, it is used to detect whether the rewriting is valid and correct.
  2. The range of thrown exception types can be narrowed, but not expanded.
    ClassNotFoundException --> Exception
  3. The return types can be the same or different. If they are different, the return type of the method rewritten by the subclass must be a subtype of the return type of the parent method.
    For example: the return type of the parent method is Person, and the return type of the subclass rewrite A class can be either Person or a subtype of Person.

Guess you like

Origin blog.csdn.net/The_onion/article/details/125031193