Jen Baby Java Basic notes polymorphism

Source: Internet
Author: User

Polymorphic
Parent class Type parent class reference =new subclass type ();
Polymorphic three-factor
1 Inheritance relations
2 to have a method rewrite
When a subclass does not override a parent class method, the child class object calls the method of the parent class
When the parent class has no methods, the parent class object cannot be used to invoke compilation without passing
When compiling, what type of object to read, and what type to access
At run time, the corresponding method is invoked according to the actual type passed in
JVM dynamic binding mechanism to invoke the appropriate method based on the actual type of runtime
3 parent class reference to child class object

Abstract methods
1. Abstract methods not specifically implemented
Access modifier abstract return type method name ();
2. The class in which the abstract method resides must be an abstract class
Access modifier abstract class class name {}
3. Subclasses must implement abstract methods in the abstract parent class, unless the subclasses themselves are abstract
4. Abstract classes can have abstract methods, or there can be no
5. Abstract classes cannot be instantiated

Upward transformation and upward transformation
Upward Transformation: The parent class's reference points to the subclass object. Automatic type conversion
Transition up:< Parent type > < reference variable name > = new < subtype > ();
The method that is called by the parent class reference variable is a subclass that overrides or inherits the parent class's method, not the parent class's method
You cannot call a method that is unique to a subclass by referring to a variable from the parent class
Downward transformation: Assigns a reference to a subclass object to the parent class reference to a child class.
That is, the parent class type is converted to a subclass type. Forced type conversion required
Subclass Type sub-class reference name = (subclass type) parent class reference;
In the process of downward transformation, a type conversion exception occurs if there is no conversion to a real subclass type
The instanceof operator is provided in Java to determine the type
if (parent class references instanceof subclass type) {
(subclass type) Subclass reference = (subclass type) parent class reference;
Subclass references. Subclass-specific methods ();
}
Parent class type As the return value of a method
Access modifier parent class type method name (parameter) {
return specific subclass object;
}

Jen Baby Java Basic notes polymorphism

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.