English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Implementation of Polymorphism in Java
What is Polymorphism
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!