JAVA polymorphism and java Polymorphism

Source: Internet
Author: User

JAVA polymorphism and java Polymorphism

 

/* Three major characteristics of multi-state OOP: encapsulation, inheritance, and Polymorphism: it can be understood as multiple forms of the existence of things, such as: animals: cats, dogs, pig: Men, female 1. polymorphism embodiment parent class reference pointing to subclass object 2. polymorphism premise (1) there must be a relationship between Class and Class, can be an inheritance relationship, it can also be the implementation relationship (2) there must be rewriting 3. The benefits of polymorphism greatly improve the scalability of the program 4. the disadvantages of polymorphism although it improves the scalability of the program, however, it can only be referenced by the parent class and accessed to the member polymorphism member variables in the parent class. Features: 1. During Compilation: see whether the referenced variable has a called method in the class to which it belongs. 2. runtime: see whether the called method exists in the class to which the object belongs * // Animal class Animal {void sing () {System. out. println ("singing") ;}// Dog class Dog extends Animal {void sing () {System. out. println ("Wangwang singing") ;}// Cat class Cat extends Animal {void sing () {System. out. println ("");} void catchMouse () {System. out. println ("rat hunting") ;}// Pig extends Animal {void sing () {System. out. println ("en sing") ;}} public class Test {public static void main (String [] args) {Cat c = new Cat (); function (c ); dog d = new Dog (); function (d); Pig p = new Pig (); function (p);} public static void function (Animal c) {c. sing ();}}

 

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.