Java Learning notes-polymorphic

Source: Internet
Author: User

The individual's understanding of polymorphism is not necessarily entirely correct, and everyone is welcome to criticize.

1. What is polymorphic

Object-oriented three main features: encapsulation, inheritance, polymorphism. In a way, polymorphism is built on the inheritance of encapsulation and inheritance.

Polymorphism can be understood as multiple forms of expression of the same thing .

1) overloading and rewriting of methods

2) The polymorphism of the subclass object: The reference to the parent class is directed to the subclass object (more of this form)

Parent Class F = new subclass ();

2. Requirements for polymorphism of subclass objects:

1) to have the inheritance of the class

2) to have a subclass override of the parent class method

3) reference to the parent class to the data object

3. Attribute is not polymorphic

Let's cover it up with a simple example:

1 //Parent Class2  Public classPerson {3      Public voideat () {4SYSTEM.OUT.PRINTLN ("People eat");5     }6      Public voidWalk () {7System.out.println ("People walking");8     }9 }Ten //sub-class man One  Public classMansextendsPerson { A      Public voideat () { -System.out.println ("Man Eats"); -     } the      Public voidWalk () { -System.out.println ("Man Walking"); -     } -      Public voidsleep () { +System.out.println ("Sleeping"); -     } + } A //sub-class woman at  Public classWomanextendsPerson { -      Public voideat () { -System.out.println ("The Woman Eats"); -     } -      Public voidWalk () { -SYSTEM.OUT.PRINTLN ("Woman Walking"); in     } -      Public voidShopping () { toSystem.out.println ("Shopping"); +     } - } the //Test Class *  Public classtest{ $      Public Static voidMain (string[] args) {Panax Notoginseng         //a reference to the parent class is directed to the child class object -Person p =NewMan ();//transition up to convert subtypes to parent type the         //virtual method invocation: Refers to the child class object entity through a reference to the parent class, and the calling method executes the method that subclasses override the parent class +P.eat ();//men Eat AP.walk ();//men Walking the         //p.sleep ();//error calling a method that does not exist in the parent class with a parent class reference +Man m = (mans) p;//A downward transition converts a parent type to a subtype, must be cast, and a reference to the parent type must point to the object of the subclass, that is, to whom to convert.  -M.sleep ();//Sleep $          $person P1 =NewWoman (); -        //java.lang.ClassCastException -        //when you are down, you can only turn to the type of object you are pointing the        //Mans m1 = (man) p1; -Woman W =(Woman) p1;Wuyi     } the}

As we can see from the above example, if you want to invoke a method that the parent class does not have, and the subclass is unique to the method, you need to force the type conversion. As above, force p to convert m.

And the conversion can only be turned to the object type, such as P1 can only be transformed downward to women M1, but not to the man type

4. Benefits of using polymorphism

1) Improved program extensibility and maintainability

2) Strong flexibility

3) Simplified code writing and modification process

 

Java Learning notes-polymorphic

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.