Detailed explanation and simple example of java polymorphism

  • 2020-06-12 08:56:28
  • OfStack

Implementation of polymorphism in Java

What is polymorphism

Object - oriented features: encapsulation, inheritance, polymorphism. From a definite point of view, encapsulation and inheritance are almost always polymorphic. This is our last concept and the most important knowledge point. Polymorphism is defined as allowing objects of different classes to respond to the same message. That is, the same 1 message can behave differently depending on the object it is sent to. (Sending a message is a function call) The technique for implementing polymorphism is called dynamic binding (dynamic binding), which means that during execution, the actual type of the referenced object is determined and its corresponding methods are called according to its actual type. The role of polymorphism: Eliminates coupling between types. In reality, the list of examples of polymorphism is endless. For example, if you press the F1 key, what pops up in the Flash interface is the help document for AS 3. If the current one under Word pops up is Word help; What pops up under Windows is Windows Help and support. The same event on different objects will produce different results.

Here are three essential conditions for polymorphic existence that you can recite in your dreams!

Three necessary conditions for polymorphic existence

1. Inheritance;
2. Rewrite;
3. Superclass references refer to subclass objects.

Advantages of polymorphism:

1. Replaceability (substitutability). Polymorphism is replaceable to existing code. For example, polymorphism works for the Circle class of circles, as well as for any other round geometry, such as circles.

2. Scalability (extensibility). Polymorphism is extensible to code. Adding new subclasses does not affect the polymorphism, inheritance, or other features of existing classes. It's actually easier to get polymorphic functionality with new subclasses. For example, on the basis of realizing the polymorphism of cone, hemicycle and hemicycle, it is easy to add the polymorphism of sphere class.

3. Interface (interface-ability). Polymorphic is a superclass that provides a common interface to subclasses by means of method signature, and is implemented by subclasses to perfect or override it. See Figure 8.3. The superclass Shape in the figure specifies two interface methods to implement polymorphism, computeArea() and computeVolume(). Subclasses such as Circle and Sphere refine or override these two interface methods in order to implement polymorphism.

4. Flexibility (flexibility). It reflects flexible and diversified operation in the application and improves the efficiency.

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

The implementation of polymorphism in Java: interface implementation, method override by inheriting the parent class, method overload by one class.

1 Question:

(1) Related 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...{} 

(2) Question: What are the following output results?


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));   1.  
    System.out.println(a1.show(c));   2.  
    System.out.println(a1.show(d));   3.  
    System.out.println(a2.show(b));   (4)  
    System.out.println(a2.show(c));   5.  
    System.out.println(a2.show(d));   6.  
    System.out.println(b.show(b));   All landowners  
    System.out.println(b.show(c));   today  
    System.out.println(b.show(d));   Pet-name ruby    

(3) Answer


 1.   A and A
 2.   A and A
 3.   A and D
 (4)   B and A
 5.   B and A
 6.   A and D
 All landowners   B and B
 today   B and B
 Pet-name ruby   A and D

Thank you for reading, I hope to help you, thank you for your support to this site!


Related articles: