Java understanding of Polymorphism

Source: Internet
Author: User

Implementation of Polymorphism in Java

What is polymorphic

    1. Object-oriented three main features : encapsulation, inheritance, polymorphism. From a certain point of view, encapsulation and inheritance are almost always prepared for polymorphism. This is our last concept, but also the most important point of knowledge.

    2. polymorphism definition : means that objects of different classes are allowed to respond to the same message. That is, the same message can be used in a variety of different ways depending on the sending object. (Sending a message is a function call)

    3. The technique of implementing polymorphism is called : Dynamic binding, which refers to the execution period judging " , eliminate the coupling relationship between types .

    4. In reality, there are numerous examples of polymorphism . For example, if you press the F1 button, if the current pop-up in the Flash interface is the as 3 help document, if Word Help is currently popping up under Word, Windows Help and Support appears under Windows. The same event occurs on different objects and produces different results.

three necessary conditions for the existence of polymorphism
One, to have inheritance;

.

Benefits of polymorphism:

1.Replaceable (substitutability). Polymorphism is replaceable for existing code. For example, polymorphism works on the Circle Circle class and works on any other circular geometry, such as a ring.
2.Extensibility (Extensibility). Polymorphism has extensibility for code. Adding new subclasses does not affect the polymorphism, inheritance, and the operation and manipulation of other attributes of existing classes. In fact, new subclasses are more likely to get polymorphic functions. For example, it is easy to add the polymorphism of sphere class on the basis of realizing the multi-state of cone, semi-cone and hemispherical body.
3.interface (interface-ability). Polymorphism is a superclass that, by way of signature, provides a common interface to subclasses, which is implemented by subclasses to refine or overwrite the class. As shown in 8.3. The super-Class shape in the figure specifies two interface methods for implementing polymorphism, Computearea () and Computevolume (). Subclasses, such as circle and sphere, refine or overwrite both interface methods in order to achieve polymorphism.
4.flexibility (flexibility). It embodies the flexible operation in the application, and improves the use efficiency.
5.simplification (simplicity). Polymorphism simplifies the process of coding and modifying the application software, especially when dealing with the operation and operation of a large number of objects.


Implementation of polymorphism in Java: interface Implementation, inheriting the parent class for method overrides, and method overloading in the same class.

Example: Class A

Package Com.yuan.test;public class A {public String show (d obj) {return ("A and D");   Public String Show (a obj) {return ("A and a"); }   }

Example: Class B

Package Com.yuan.test;class B extends a{public String show (b obj) {return ("B and B");          Public String Show (A obj) {return ("B and A"); }   }

Instance Class C

Package Com.yuan.test;public class C extends b{}

Instance Class D

Package Com.yuan.test;public class D extends b{}

Test class:

Package com.yuan.test;public class abcd {public static void main (String[]  args)  {         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)); //a  a          System.out.println (A1.show (c)); //a a           System.out.println (A1.show (d)); //a d          System.out.println (A2.show (b));  // b a&nbsP;        system.out.println (A2.show (c)); // b a          system.out.println (A2.show (d)); // a d          system.out.println (B.show (b));//b b          system.out.println (B.show (c)); //b b          system.out.println (B.show (d));  //a d}}


Java understanding of Polymorphism

Related Article

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.