Detailed explanation of polymorphic concept and implementation principle in java

  • 2020-06-23 00:25:07
  • OfStack

1. What is polymorphism?

1. Definition of polymorphism

Allows objects of different classes to respond to the same 1 message. That is, a message can behave in many different ways depending on the object it is sent to (sending a message is a function call)

2. The role of polymorphism

Eliminates coupling between types

3. Description of polymorphism

Modern network novels abound, we can use it to give an example

One day you see multiple novels being updated simultaneously on your phone, such as The Legend of The Overlord, The Snow Eagle Lord, the Dragon King... Here we can describe it as follows:

Fiction a= The Great Master

b= Snow Eagle Lord

Fiction c= Legend of the Dragon King

...

Polymorphic, the Overlord, the Snow Eagle Lord, and the Dragon King are all subclasses of novels. We can refer to different subclasses only through the novel superclass. This is polymorphic

Of course, this understanding is far from enough. To understand polymorphism, the first step is to understand the "upward transition".

In the above example, the novel (XS) is the parent class, the Grand Master (DZZ), the Snow Eagle Lord (XYLZ), and the Dragon King legend (LWCS) are subclasses, so we define the following code

DZZ a=new DZZ();

This code should be familiar to all but instantiate a dominant object.

XS a=new DZZ();

The way we understand it here is that an a of type XS is defined to point to an instance of the DZZ object. Since DZZ inherits from XS, DZZ can automatically transition upward to XS, so a can point to DZZ instance objects. In inheritance, we know that a subclass is an extension of the superclass, which can provide more powerful functions than the superclass. If we define a superclass reference type that refers to the subclass, then it can refer to the generality of the superclass and also use the powerful functions of the subclass

However, there is one drawback to the upward transition, that is, it must lead to the loss of methods and attributes that we cannot obtain. So the application of a superclass type can call all the properties and methods defined in the superclass, but only those that exist in subclasses


public class XS {

    public void fun1() {
      System.out.println("XS In the fun1");
      fun2();
    }


  public void fun2() {
     System.out.println("XS In the fun2");    
  }
}


public class DZZ extends XS{

  /*
   *  Subclasses override superclass methods 
   *  The method does not exist in the parent class, and cannot be referenced by the parent class after upward transformation 
   */
   public void fun1(String a) {
     System.out.println("DZZ In the fun1");
     fun2();
   }

   /*
    *  Subclasses override superclass methods  
    *  The call must use this method 
    */
   public void fun2() {
     System.out.println("DZZ In the fun2");
   }
}


public class DuoTaiTest {

   public static void main(String[] args) {
     XS a=new DZZ();
     a.fun1();
  }
}
output:
XS In the fun1
DZZ In the fun2

Therefore, for polymorphism, we can summarize as follows:
A superclass reference to a subclass, due to its upward transformation, can only access methods and properties owned in the superclass, but for methods that exist in the subclass but do not exist in the superclass, the reference cannot be used, even if overloading the method. If a subclass overrides some method in the parent class, the methods defined in the subclass must be used when the methods are called (dynamic concatenation, dynamic invocation).

For object orientation, polymorphism is divided into compile-time polymorphism and run-time polymorphism. During editing polymorphism is static, which mainly refers to the overloading of methods. It is to distinguish different functions according to the difference of parameter list. Runtime polymorphism, on the other hand, is dynamic and is implemented by dynamic binding, which is what we call polymorphism.

2. Implementation of polymorphism

1. Implementation conditions

At the very beginning razor has inherited in preparation for the implementation of the polymorphic state. Child subclass inherits parent Father, we can write a point to subclass the superclass type reference, this reference can handle both parent Father object, can also handle subclasses Child object, when the same message is sent to subclass or superclass object, the object will be according to own reference and perform different actions, this is polymorphic. Polymorphism is the same message that causes different classes to respond differently

Java There are three necessary conditions for polymorphism: inheritance, rewriting, and upward transformation

Inheritance: In polymorphism, there must be a subclass and a parent with an inheritance relationship

Override: A subclass redefines some of the methods in the superclass, and when these methods are called, the subclass's methods are called

Upshift: In polymorphism, you need to assign a subclass reference to a superclass object so that the reference has the ability to call both superclass and subclass methods

Only when the above three conditions are met can we use the same logical implementation code in the same inheritance structure to process different objects and thus perform different behaviors

Its polymorphism implementation mechanisms for Java follow one principle: when the superclass object reference variable reference subclass object, is the type of the reference object rather than a reference variable type whose members decided the call method, but this is the method called must be defined in a superclass, method that is covered by a subclass

2. Implementation form

inheritance


public class XS {

      private String name;

    public String getName() {
      return name;
    }

    public void setName(String name) {
      this.name = name;
    }

    public XS() {

    }

    public String drink() {
      return " The name of the novel you read :"+getName();
    }

    public String toString() {
       return null;
    }


}
public class DZZ extends XS{


  public DZZ() {
    setName("DZZ");
  }

  public String drink() {
    return " The name of the novel you read :"+getName();
  }

  public String toString() {

    return " The novel, :"+getName();
  }
}
public class XYLZ extends XS{

  /**
   * 
   */
  public XYLZ() {
     setName("XYLZ");
  }

  public String drink() {
    return " The name of the novel you read :"+getName();
  }

  public String toString() {

    return " The novel, :"+getName();
  }
}   
public class DuoTaiTest {
    public static void main(String[] args) {
      XS [] xs=new XS[2];

      DZZ a=new DZZ();
      XYLZ b=new XYLZ();

      xs[0]=a;
      xs[1]=b;

      for(int i=0;i<2;i++) {
       System.out.println(xs[i].toString()+"::::"+xs[i].drink());
      }

      System.out.println("-------------------");
  }
}
ouput:
 The novel, :DZZ:::: The name of the novel you read :DZZ
 The novel, :XYLZ:::: The name of the novel you read :XYLZ
-------------------

In the above code, DZZ,XYLZ inherit XS and override drink(),toString(). The result of the program is to call the method in the subclass and output the name of DZZ,XYLZ. This is the manifestation of polymorphism. Different objects can perform the same behavior, but they all need to perform it in their own way, thanks to the upward transition

We all know that all classes inherit from the superclass Object, and the toString() method is also the method in Object, when we write this:


Object o = new DZZ();
System.out.println(o.toString());

output:
 The novel, :DZZ

Object,XS,DZZ3 :DZZ -- > XS - > Object. So we can say this: when a subclass overrides a superclass method, only the last method in the object inheritance chain is called. But notice if you write it this way:


Object o = new xs();

System.out.println(o.toString());

output:
null// because DZZ Does not exist in the object inheritance chain 

So the inheritance implementation-based polymorphism can be summarized as follows: for the parent type of a reference subclass, it applies to all subclasses of that parent class when the reference is processed. The implementation of the method is different depending on the subclass object, and the behavior resulting from performing the same action is different.

If the parent class is an abstract class, then a subclass must implement all the abstract methods in the parent class, so that all the subclasses of the parent class 1 must have an external interface to one, but the concrete implementation inside can vary. This allows us to use the unified 1 interface provided by the top-level class to handle methods at that level.

The contents of this article hope to give a friend in need some help


Related articles: