Java polymorphic of Power node Java College collation

  • 2020-06-19 10:18:07
  • OfStack

What is polymorphism

1. Three features of object-oriented: encapsulation, inheritance and 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.

2. Definition of polymorphism: allows 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)

3. 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 the corresponding method is called according to its actual type.

4. The role of polymorphism: eliminating coupling between types.

5. In reality, the examples of polymorphism are too numerous to mention. For example, if you press the F1 key, what pops up in the Flash interface is the help document for AS 3. If what pops up under Word 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 necessary conditions for polymorphic existence, best remembered on the basis of understanding.

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 circular 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 (ES43en-ES44en) 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 rewriting by inheriting the parent class, method overloading in the same 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


Related articles: