We know that overloaded methods are not called polymorphic and get resolved at compile time and this is why sometime method overloading is also known as compile time polymorphism or early/static binding.
1 | javap -verbose OverridingInternalExample |
Bytecode for anyMammal.speak()
and humanMammal.speak()
are the same ( invokevirtual #4 // Method org/programming/mitra/exercises/OverridingInternalExample$Mammal.speak:()V
) because according to compiler both methods are called on Mammal
reference.
JVM uses the invokevirtual
instruction to invoke Java equivalent of the C++ virtual methods.
The Java virtual machine does not mandate any particular internal structure for objects 4.
根据vstable来算,
There is only one vtable
per class means it is unique and same for all objects of a class similar to Class
object.
Now instead of creating new entries for these methods with updated references. JVM will modify the references to the already present methods on the same index where they were present earlier and will keep the same method names.