This is the fourth day of my participation in Gwen Challenge

Class of polymorphism

Polymorphic: A type declared variable that can refer to multiple objects of actual type

There are three preconditions for the realization of polymorphism.

  1. Inheritance: A subclass inherits attributes and methods from its parent class
  2. Method override: A subclass overrides a parent class method
  3. Upcast: Subclass instance object assigned to parent class reference variable
  • Subclass instance object:new Son();
  • Parent class reference variable:Father father;

Simply put, a subclass overrides the instance methods of its parent class and assigns values to its parent variables. This is the polymorphic, multiform form of a class

The polymorphism of the class is divided into two important points (it is important to note that the core of the polymorphism understanding)

  1. The upward and downward transition of polymorphism
  2. Static and dynamic binding of polymorphism

The upward transition of polymorphism

Polymorphic upward transformation: when a subclass instance variable is assigned to a parent class reference variable

Father father = new Son();

Upward transition of polymorphism :(to form polymorphism)

  1. A superclass reference variable can call a superclass method overridden by a subclass
  2. A superclass reference variable may not call a method unique to a subclass

The downward transformation of polymorphism

Polymorphic downward transformation: the upward transformation of the parent class reference variable and strong transfer of the subclass reference variable

Son son = (Son) father;

The downward transformation of polymorphism, in essence, is to transform into the original type, after the downward transformation does not constitute the polymorphism of the class

Downward transformation of polymorphism :(does not constitute polymorphism)

  1. It must be premised on the upward transition of polymorphism (first down, then up)
  2. It must be cast in the downward casting process
  3. To avoid cast exceptions, use the keywordinstanceofjudge
  4. After a polymorphic downward transformation, subclass reference variables can call subclass-specific methods

instanceofThe keyword is used to compare the inheritance relationship between two classes

Static binding of polymorphism

Static binding: methods determined at program compile time (static methods of the parent class that cannot be overridden by subclasses)

Compile-time type variable = new runtime type ();

A static method of the same name as the parent class, depending on whether the subclass exists

  • If the subclass has a static method of the same name, call the subclass implementation (nearest rule, based on the instance object of new).
  • If the subclass does not have a static method of the same name, the parent class implementation is called

Dynamic binding of polymorphism

Dynamic binding: Properties and methods determined at runtime, based on the right run-time type

Static binding is for static class properties, while dynamic binding is for instance properties, instance methods

When the parent class of polymorphic upward transformation references variables and calls properties and methods of the same name in the parent class and subclass, the parent class method (instance properties and instance methods) overwritten by the subclass is actually called.

A class that overrides toString() and is called through that class Object is no longer Object

A subclass overrides the instance method of the parent class, and if the right-hand instance object is a subclass, the implementation of the subclass is called

Order of execution in inheritance

In Java inheritance, the various attributes, methods, and code blocks of the parent and child classes have different execution priorities

If the attributes, methods, and code blocks have the same priority, they are executed in sequence

  1. Superclass static fields, static code blocks
  2. Subclass static field, static code block
  3. Parent class member variables, non-static code blocks
  4. Parent class constructor
  5. Subclass member variables, non-static code blocks
  6. Subclass constructor

To put it simply, the static parts of the parent and subclass are the first priority, followed by the rest of the parent and the subclass

It is important to note that constructor execution takes precedence over instance properties and methods

This is also done for the convenience of assigning values to instance properties of objects via constructors