The polymorphism of Java

Source: Internet
Author: User

① What is polymorphic?
Polymorphism (polymorphism) is characterized by its ability to behave in many forms. The same implementation interface that uses different instances to perform different operations

② What is the upward transformation? What are the grammatical requirements?
1, the reference of a parent class to a subclass object, called upward transformation (upcasting), at this time through the parent class reference variable call method is a child class overrides or inherit the parent class method, not the parent class method, at this time through the parent class reference variable cannot call subclass specific method
< parent type > < reference variable > = new < subtype > ()

Automatic type conversion

③ what is the downward transformation? What are the grammatical requirements?
A reference to a subclass that refers to a parent class reference to a subclass object, called a downward Transformation (downcasting), can access a method specific to a subclass. Must be converted to the real subclass type pointed to by the parent class, or a type conversion exception will occur classcastexception

Forcing type conversions

What are the three conditions for ④ to achieve polymorphism?
1, the existence of inheritance (inheritance is the basis of polymorphism, no inheritance there is no polymorphism)
2, subclasses overriding the parent class method (polymorphic call subclasses after overriding the method)
3. Parent class reference variable points to subclass object (subclass to parent class conversion)

⑤ What are the benefits of using polymorphism?
Reducing the amount of code in a class can improve code scalability and maintainability

What is the function and syntax format of the instanceof operator?
Determines whether an object belongs to a class or implements an interface
Boolean result = Object instanceof Class (interface)

Note: When using the instanceof operator, the object must be of the same type as the class or interface specified by the second parameter of Instanceof, or a compilation error will occur, and instanceof is typically used in conjunction with coercion type conversions


The polymorphism of 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.