Virtual method in Java

Source: Internet
Author: User

Today, when I look at the standard of JCVM, I see that there is a virtual method, and I feel puzzled when I looked at Java before and did not find such a method.

Baidu, Google a bit, only to find that Java is thecommon method is virtual methods, dynamic binding is the default behavior of Java .

If you don't want a method to be virtual, just declare this method final.

As for the virtual method in C + +, refer to the following article

Http://www.cnblogs.com/xd502djj/archive/2010/09/22/1832912.html

In many blogs there is a saying thatJava's virtual method is actually dynamic binding/dynamic dispatch.

(Http://stackoverflow.com/questions/9453701/what-is-virtual-method-calling-in-java)

As you can see from the answer,virtual method is for polymorphism.

The following is a brief introduction to dynamic method scheduling.

In Java, if a method of a subclass has the same name and type signature as a method of a superclass, the method in the class called subclasses overrides the corresponding method in the superclass .

When an overridden method is called in a subclass, the version defined by the subclass is always called, and the version defined by the superclass is hidden.

method rewriting forms the basis of dynamic method dispatch, and dynamic method scheduling can be said to be the most powerful function in Java .

Dynamic methods control room a mechanism through which calls to overridden methods can be resolved at run time, rather than at compile time.

dynamic method Scheduling is the mechanism in which Java implements polymorphism .

The first thing to assert is that a superclass reference variable can point to a subclass object . Java leverages this fact to resolve calls to overridden methods at run time.

Here is the implementation principle:

When the overridden method is called through a superclass reference, Java determines which version of the method is called based on the type of object referenced at the time of invocation. Therefore, this decision is made at run time.

If you reference different types of objects, you will invoke different versions of the method.

That is, the type of the object that is currently being referenced (rather than the type of the reference variable) determines which version of the override method will be executed.


Examples are as follows:


public class Test {

public static void Main (string[] args) {

SuperTest a = new SuperTest ();
A.eat ();

SuperTest B = new SuperTest1 ();
B.eat ();

SuperTest C = new SuperTest2 ();
C.eat ();

}

}


Class supertest{

void Eat () {
System.out.println ("This is the SuperTest print");
}
}

Class SuperTest1 extends supertest{
void Eat () {
System.out.println ("This is the SuperTest1 print");
}
}

Class SuperTest2 extends supertest{
void Eat () {
System.out.println ("This is the SuperTest2 print");
}
}

The result of the final output is:

This is the SuperTest print
This is the SuperTest1 print
This is the SuperTest2 print

Virtual method in Java

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.