Parent class
========================================================== ======================================
Public Class fruit {
Public void eat (){
System. Out. println ("I wanna eat fruit ");
}
}
-----------------------------------------------------------------------------
Public class AppleExtendsFruit {
Public void eat (){
System. Out. println ("I wanna eat apple ");
}
}
Public class orangeExtendsFruit {
Public void eat (){
System. Out. println ("I wanna eat orange ");
}
}
Bytes --------------------------------------------------------------------------------------
Test class
Bytes -------------------------------------------------------------------------------------
Public class testbind {
Public static void chose (fruit f ){
F. Eat ();
}
/**
* @ Param ARGs
*/
Public static void main (string [] ARGs ){
// Todo auto-generated method stub
Fruit A = new Apple ();
Fruit o = new orange ();
Testbind. chose ();
Testbind. chose (O );
}
}
Output result:
I wanna eat apple
I wanna eat orange
Bytes --------------------------------------------------------------------------------------
Dynamic binding: determines the actual type of the referenced object during execution (not during compilation) and calls the corresponding method based on the actual type.
The code above decides which instance method to call during the execution period.
--------------------------------------
In Java, except the static and final methods, all other methods are bound at runtime (also called post-binding. [Cannot be overwritten]
Private methods are implicitly specified as final. [The subclass cannot override the private method of the parent class]
Therefore, the final method is not bound at runtime.