Overloading and rewriting instance code of Java functions

In Java, polymorphism can be divided into two types: compile time polymorphism (overloading) and run-time polymorphism (Rewriting). Compile time polymorphism is also called pre binding and run-time polymorphism is also called post binding.

The following is an example:

It should be noted here:

For a A2 = New B ();

If you print A2 separately, the result is B@ (hash code) instead of A@ (hash code), but this does not mean that the type of A2 is B type, as we call a2. in the above program. print(a2); The output result is B and a instead of a and a (if A2 is assumed to be of type B, the print method with parameter B in class A should be called, because there is no such method, then the second is to call the method with parameter a should output a and a, because B is a subclass of a).

The above is the example code analysis of rewriting and overloading. I hope it will be helpful to students learning Java.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>