Java Personal Learning Note: polymorphic

Source: Internet
Author: User

Polymorphism is the most important concept in object-oriented.

Polymorphism occurs when an object, compile-time type and run-time type are inconsistent. Or, when an object, using the same method, has a different running state, indicating that there is polymorphism.

Two types of polymorphism:

Overloading and overriding of methods

Object polymorphism (can be applied directly on abstract classes and interfaces)

Related concepts:

There are two types of reference variables in Java: Compile-time type and run-time type.

A compile-time type is determined by the type used when declaring the variable, and the run-time type is determined by the object that is actually assigned to the variable.

Object Polymorphism in Java, the object of a subclass can be used instead of the object of the parent class.

A variable can only be determined by a type of verse

A reference variable may point to (reference) many different types of objects.

Subclasses can be thought of as special parent classes, so a reference to a subclass type can point to the object of a subclass: transform up.

If a reference-type variable is declared as the type of the parent class, but the child-class object is actually referenced, the variable cannot be added to the properties and methods in the access subclass.

The property is determined at compile time, so the subclass is the runtime type and the added variable does not exist.

method is dynamically bound, and the method's invocation is determined at run time.

Multi-State implementation

There is a need for inheritance or implementation relationships.

To have an overwrite operation.

Member method, compile time: To see if the class to which the reference variable belongs is yet to invoke the method.

Runtime: Invokes the overridden method in the class that actually belongs.

Member variables are not polymorphic, only the class to which the reference variable belongs.

instanceof Test A variable is a return Boolean for an object of XX

The two classes required to be compared must be a relationship between the subclass and the parent class, or there will be a compilation error.

x instanceof A If x belongs to sub-class B x instanceof a value of a is also true

Java Personal Learning Note: 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.