English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Detailed Explanation and Simple Examples of Polymorphism in Java

Implementation of Polymorphism in Java

What is Polymorphism

  1. The three major characteristics of object-oriented: encapsulation, inheritance, and polymorphism. From a certain perspective, encapsulation and inheritance are almost prepared for polymorphism. This is our last concept and the most important knowledge point.
  2. The definition of polymorphism: refers to allowing objects of different classes to respond to the same message. That is, the same message can adopt various different behavior modes according to the sending object. (Sending a message is a function call)
  3. The technology for implementing polymorphism is called: dynamic binding (dynamic binding), which refers to the judgment of the actual type of the referenced object during execution, and the corresponding method is called according to its actual type.
  4. The role of polymorphism: eliminate coupling relationships between types.
  5. In reality, there are countless examples of polymorphism. For example, pressing F1 this action, if it pops up AS in the Flash interface 3 help documentation; if the help that pops up when in Word is Word help; if it pops up in Windows, it is Windows help and support. The same event occurring on different objects will produce different results.

Below are the three necessary conditions for the existence of polymorphism, which require everyone to recite them even in dreams!

Three necessary conditions for the existence of polymorphism

One, there must be inheritance;
Two, there must be overrides;
Three, the superclass reference points to the subclass object.

 Advantages of polymorphism:

1.Substitutability (substitutability). Polymorphism has substitutability for existing code. For example, polymorphism works for the Circle class, and it also works for other circular geometric bodies, such as annulus.

2.Extensibility (extensibility). Polymorphism has extensibility for code. Adding new subclasses does not affect the polymorphism, inheritance, and other characteristics of existing classes. In fact, new subclasses are more likely to obtain polymorphism functionality. For example, on the basis of polymorphism of cone, frustum, and hemispherical, it is easy to add the polymorphism of the spherical class.

3.Interface (interface)-.Interface (interface). Polymorphism is realized by the superclass providing a common interface through method signature, which is improved or overridden by subclasses.8.3 .As shown in the figure, the superclass Shape defines two interface methods for implementing polymorphism, computeArea() and computeVolume(). Subclasses, such as Circle and Sphere, improve or override these interface methods to achieve polymorphism.

4.Flexibility (flexibility). It demonstrates flexible and diverse operations in applications, improving efficiency.

5.Simplicity (simplicity). Polymorphism simplifies the process of coding and modifying application software, especially when dealing with a large number of objects, this feature is particularly prominent and important.

The implementation methods of polymorphism in Java: interface implementation, method overriding by inheriting the parent class, and method overloading within the same class.

A small topic:

(I) Relevant Classes

class A ...{ 
     public String show(D obj)...{ 
        return ("A and D"); 
     }  
     public String show(A obj)...{ 
        return ("A and A"); 
     }  
}  
class B extends A...{ 
     public String show(B obj)...{ 
        return ("B and B"); 
     } 
     public String show(A obj)...{ 
        return ("B and A"); 
     }  
} 
class C extends B...{}  
class D extends B...{} 

(Two) Question: What is the following output result?

A a1 = new A(); 
    A a2 = new B(); 
    B b = new B(); 
    C c = new C();  
    D d = new D();  
    System.out.println(a1.show(b));  ① 
    System.out.println(a1.show(c));  ② 
    System.out.println(a1.show(d));  ③ 
    System.out.println(a2.show(b));  ④ 
    System.out.println(a2.show(c));  ⑤ 
    System.out.println(a2.show(d));  ⑥ 
    System.out.println(b.show(b));  ⑦ 
    System.out.println(b.show(c));  ⑧ 
    System.out.println(b.show(d));  ⑨   

(Three) Answer

①  A and A
②  A and A
③  A and D
④  B and A
⑤  B and A
⑥  A and D
⑦  B and B
⑧  B and B
⑨  A and D

Thank you for reading, I hope it can help everyone. Thank you for your support of this site!

You May Also Like