Java records -15-of object-oriented polymorphism continuation

Source: Internet
Author: User

The Java object-oriented polymorphic continuation

    1. Polymorphic: A reference to a parent type can point to an object of a subtype.

    2. Late binding is the determination of the type when executing the run, not the time of compilation.

    3. Parent p = new Child (); When calling a method using polymorphic mode, first check if the parent class has the method such as sing (), if there is no compile error, if any, then call the subclass of the method, such as sing ().

    4. There are two types of coercion type conversions:

      1). Up-type conversion (UPCAST): For example, convert the cat type to the animal type and convert the subtype to the parent type. For up-type conversions, you do not need to specify explicitly.

      2). Down type conversion (downcast): For example, convert the animal type to cat type. Converts the parent type to a subtype. For down conversions, you must explicitly specify (you must use coercion type conversions).

    5. Polymorphic Classic Use:

public class car{

public void Run () {}

}

public class BMW extends car{

public void Run () {}

}

public class VW extends car{

public void Run () {}

}

public class testcar{

public static void Main (string[] args) {

Testcar TC = new Testcar ();

Car BMW = new BMW

Tc.test (BMW);

Car VW = new VW ();

Tc.test (VW);

}

public void test (car car) {

Car.run ();

}

}

The test () method receives a car parent class parameter and calls its method. But when actually used, the object that is passed is the subclass, which corresponds to the method that calls the subclass. This allows subclasses to use this method.

Java records -15-of object-oriented polymorphism continuation

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.