The understanding of polymorphism in Java

Source: Internet
Author: User
Object-Oriented: class: A concrete object abstract that can be abstracted to represent a class of object objects with the same characteristics and behavior: is instantiated by the class, the object is an instance of the class Java in the understanding of polymorphism to make an analogy of the father person has behavior this method, which includes several actions: Eat, sleep, walk father has three sons, three sons have inherited the father's behavior method, so three sons have to eat, sleep, walk these movements, but three sons have their own action--the big son A will play guitar, the second son B will sing, three sons C will drum ... 1.Person person = new A (); Not a parent class object that points to a subclass reference but a parent class reference to a Subclass object 2. This object cannot call subclass a specific guitar method--person.guitar (); X3. If this is the only program that is not polymorphic, remember to implement the three elements of polymorphism: Inheritance overrides the parent class reference to the Subclass object 4. After that, if you call Persion.eat (), at this point in the code's compilation phase, the Persion call is still itself eat (), not the son's. And when the program is running, is Java XXX, Persion call is the son of Eat (). This dynamic process is polymorphic. In Java, a reference to a parent class can point to either the object of the parent class or the object of the child class. However, a reference to a subclass cannot point to the object of the parent class. Reference types can also be type-cast. However, the type of conversion must have an inheritance relationship, which is to allow conversions between parent and child classes only. If you attempt to convert two types that are unrelated, a compilation error will be thrown. You can use instanceof to determine whether a reference is a specified type.

The understanding of polymorphism in Java

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.