I learned a lot about how C++ manages its virtual tables in the presence of inheritance (multiple, virtual etc.) and how it lays the objects in memory.
Now, Java only has to worry about single line of inheritance, no instance method hiding etc. so virtual tables should be a bit simpler in this case. I know that Class
files act as "gateways" to method area, where type definitions are stored, including method bytecode I presume. The following two questions come to my mind:
Class
objects?Having the following classes and instantiations:
class A{ int f(){...} }
class B extends A{ int f(){...} }
A a = new B();
a.f();
f() in B is called. Is A resolving through Class
file of B the right method pointer?
Thanks a lot in advance for your comments.
The call to a.f()
is realized in the java bytecode assembly language as:
aload_1 // load the local variable a, here from local address 1
invokevirtual with index into the constant pool for
method ref:
class "A"
nameAndType "f", "()I"
At run-time possibly the vtable then solves the call to B.f(). But as you may see, the class format is quite abstract, and the JVM has all liberty on loading the class for an "efficient" implementation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With