Implementation of Java Runtime polymorphism

Source: Internet
Author: User

Intera;

A = new B ();

A. Fun ();

A = new C ();

A. Fun ();

}

}

Output result:

This is B

This is C
In the preceding example, class B and class C are two classes that implement interface intera, respectively implementing the interface method fun (), by assigning instances of class B and class C to interface reference A, the dynamic binding of methods during runtime is realized, making full use of "one interface, multiple methods "demonstrate the dynamic polymorphism of Java.

Note that when Java uses interface variables to call its method for implementing class objects, this method must have been declared in the interface, in addition, in the interface implementation class, the types and parameters of the implementation method must be exactly matched with the defined in the interface.

Conclusion: The above is the implementation method of Java Runtime polymorphism. You can use it flexibly in the programming process, but it has high performance requirements.CodeWe do not advocate the use of Runtime polymorphism. After all, the system overhead of dynamic method calling in Java Runtime is relatively large than that in common method calling.

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.