Five, inheritance and polymorphism

inherit

Class inheritance

The inheritance mechanism can derive new classes on the basis of existing classes, which have the attributes and behaviors of existing classes, and can also add new attributes and behaviors as needed

The inherited class is called the subclass (derived class), and the inherited class is called the parent class (base class or super class)

Format: [modifier] class subclass name extends parent class name {…}

note:

  • If the child class and the parent class are in the same package, the child class naturally inherits the member variables and member methods of the parent class that are not private. The access rights of inherited member variables or methods remain unchanged
  • If the child class and the parent class are not in the same package, the child class inherits the protected and public member variables and methods of the parent class as its own member variables and member methods. The access rights of inherited member variables or methods remain unchanged
  • The child class cannot inherit the private properties of the parent class

super keyword

​ Need to be called during the construction of the subclass and must call the construction method of the parent class:

  • Implicitly call the parent class constructor: call the parent class constructor without parameters

  • Display call parent class constructor

    Format: super (parameter 1, parameter 2,...)

If you want to use the properties or methods inherited from the parent class in the subclass, especially the properties or methods hidden by the subclass, you can use the keyword super

The difference between super and this

Member variables Member method Construction method
this this. member variable this. member method (parameter 1, parameter 2, ...) this(parameter,...)
this Member variables that refer to instances of the current class Call the member method of an instance of the current class Call other construction methods of the current class
super super. member variable super. member method (parameters,...) super(parameter,...)
super Refer to member variables inherited from the parent class Refer to member methods inherited from the parent class Call the constructor of the parent class

Method overriding (overriding)

The subclass can redefine the method with the same name as the parent class, and the two methods are of the same type, the number of parameters in the method is the same, and the type of the parameters is the same

The method defined by the subclass is the same as the method of the parent class (name, number of parameters, parameter type)

note

  • When overriding the method of the parent class, the access authority of the method cannot be reduced, but the access authority can be increased

In inheritance, the subclass redefines a variable that is exactly the same as the member variable of the inherited class from the parent class, which is called the hiding of the member variable, but the subclass can call the method inherited from the parent class to operate the hidden member variable

The difference between method override and method overload

Point of comparison Overload Rewrite
word Overload Override
Method declaration The method name is the same, but the parameter list is different The method name is the same, the parameter list is the same
return value No requirement The return value must be the same or subclass
access permission No requirement The child class cannot be more strict than the parent class
range In the same category Inheritance relationship

Polymorphism

Polymorphism is: when a class has many subclasses, and these subclasses override an instance method in the parent class, then when we put the object reference created by the subclass into the parent class type, we get An upcast object of the object

Polymorphism means that when an instance method of a parent class is overridden by its subclasses, it can produce its own functional behavior in various ways

The necessary conditions for polymorphism:

  • inherit
  • Rewrite
  • Upward transformation

Up-casting is automatic, down-casting is mandatory

package

Why do you need a package?

  1. In order to solve the problem of duplicate names between classes
  2. Easy to manage

final

A class that uses final modification is called a final class, which cannot be inherited, that is, it cannot have subclasses

For example, the System class and String class provided by the Java language are both final classes

Guess you like

Origin blog.csdn.net/weixin_42248871/article/details/115323485