Java polymorphism during runtime

Source: Internet
Author: User

Runtime polymorphism is the most powerful mechanism for code reuse in Object-Oriented Programming. Dynamic concepts can also be said to be "one interface, multiple methods ". Dynamic Method scheduling is the basis for Java Implementation of Runtime polymorphism. It is a mechanism for calling overloaded methods at runtime rather than during compilation, next we will talk about the implementation of polymorphism in java runtime in inheritance and interface implementation.

1. inherit from super class objects and reference variables to reference subclass objects.

Example:

// Define superclass superA
Class superA
{
Int I = 100;
Void fun ()
{
System. out. println ("This is superA ");
}
}
// Define subB, a subclass of superA
Class subB extends superA
{
Int m = 1;
Void fun ()
{
System. out. println ("This is subB ");
}
}
// Define subC, a subclass of superA
Class subC extends superA
{
Int n = 1;
Void fun ()
{
System. out. println ("This is subC ");
}
}

Class Test
{
Public static void main (String [] args)
{
SuperA;
SubB B = new subB ();
SubC c = new subC ();
A = B;
A. fun (); (1)
A = c;
A. fun (); (2)
}
}

The running result is:

This is subB
This is subC

In the above Code, subB and subC are sub-classes of superA. In the Test class, we declare three reference variables a, B, c, you can call a dynamic method by assigning a subclass object reference to a variable referenced by a superclass object. Someone may ask: "Why (1) and (2) do not output: This is superA ". This mechanism of java follows the principle that when a super-Class Object references a variable to reference a subclass object, the type of the referenced object rather than the type of the referenced variable determines who calls the member method, however, the called method must have been defined in the superclass, that is, the method covered by the quilt class.

Therefore, do not be confused by (1) and (2) in the preceding example. fun (), but because a in (1) is assigned a value by B and points to an instance of subB, (1) the fun () called () actually, it is the member method fun () of the subB subclass. it overwrites the member method fun () of the superclass superA. Similarly, (2) it calls the member method fun () of the subC subclass ().

In addition, if the subclass inherits an abstract class, although the abstract class cannot be instantiated through the new operator, you can create an abstract class Object Reference pointing to the subclass object to achieve Runtime polymorphism. The specific implementation method is the same as above.

However, the subclass of an abstract class must overwrite all the abstract methods in the super class. Otherwise, the subclass must be modified by the abstract modifier and cannot be instantiated.

Related Article

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.