Java polymorphism understanding and java polymorphism understanding

Source: Internet
Author: User

Java polymorphism understanding and java polymorphism understanding

Implementation of polymorphism in Java

What is polymorphism

The following are three necessary conditions for the existence of polymorphism, which require that everyone be able to carry out their dreams!

Three necessary conditions for Polymorphism
1. There must be inheritance;
2. There should be rewriting;
3. parent class references to child class objects.

Benefits of polymorphism:

1. substitutability ). Polymorphism can replace existing code. For example, polymorphism works on circles and on any other circular ry, such as rings.
2. extensibility ). Polymorphism is extensible for code. Adding a new subclass does not affect the polymorphism, inheritance, and other features of the existing class. In fact, the new subclass is easier to obtain the polymorphism function. For example, on the basis of the polymorphism of cone, semi-cone, and semi-sphere, it is easy to add the polymorphism of sphere.
3. interface-ability ). Polymorphism provides a common interface to a subclass through method signature, which is implemented by the subclass to improve or overwrite it. 8.3. The super-class Shape in the figure specifies two interface methods that implement polymorphism, computeArea () and computeVolume (). Subclass, such as Circle and Sphere. In order to realize polymorphism, perfect or override these two interface methods.
4. flexibility ). It reflects flexible and diverse operations in applications and improves the efficiency of use.
5. simplicity ). Polymorphism simplifies the process of writing and modifying application software code, especially when processing a large number of object operations and operations, this feature is particularly prominent and important.


Implementation of polymorphism in Java: interface implementation, inheriting the parent class for method rewriting, and carrying out method overloading in the same class.

A small 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 output results below?

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));    ⑨     

 

(3) 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

 

Notes for learning Java !!!
If you have any questions or want to obtain learning resources during the learning process, join the Java learning exchange group: 159610322 let's learn Java together!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.