Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java vtable for super methods

When we have two classes:

class Foo {
    void foo() {
        System.out.println("foo");
    }
}

and:

class Bar extends Foo{
    void bar() {
        System.out.println("bar");
    }
}

Does the Bar clazz object stores references to both foo() and bar() methods in vtable or there is just reference to bar() method in vtable of Bar and to access the foo() method jvm access the Bar clazz object, then Foo clazz object and then finds the foo() method in its vtable? Is it more like this: enter image description here or that: enter image description here Or maybe this is not described in specification and can depend on JVM implementation?

like image 252
swch Avatar asked Mar 07 '26 12:03

swch


1 Answers

JVM specification does not prescribe how to implement virtual method calls. It does not even refer to a notion of vtable. JVM implementation may choose this or another way as long as it behaves as expected.

As to HotSpot JVM, the reference implementation of Java SE virtual machine, it works like in your first picture, i.e. a single vtable of a class contains all its virtual methods including inherited.

     ------------------      ------------------ 
    | Foo.class vtable |    | Bar.class vtable |
    |------------------|    |------------------|
    | clone            |    | clone            | \
    | equals           |    | equals           | | java.lang.Object
    | hashCode         |    | hashCode         | / virtual methods
    | ...              |    | ...              |
    | foo              |    | foo              | } Foo virtual methods
     ------------------     | bar              | } Bar virtual methods
                             ------------------

Such layout guarantees that all descendants of Foo class will have the reference to foo method at the same index in vtable. This allows to make virtual calls fast enough, i.e. in a constant time even for megamorphic methods.

like image 192
apangin Avatar answered Mar 09 '26 00:03

apangin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!