5

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:

  1. Is there any vtable/method table structure in Java in general? How is it stored and linked to Class objects?
  2. How is inheritance/dynamic method call solved? What I mean is:

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.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
Bober02
  • 15,034
  • 31
  • 92
  • 178

3 Answers3

4

In Java only what it must do is specified, how it does it is not. This has some advantages in that the code can be optimised in ways C++ doesn't allow. For example, "virtual" methods can be inlined by the JVM even if they are from a different library/jar.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • OK, any possible solutions you know of that has been successful? how does SUN do it? – Bober02 Apr 19 '12 at 10:04
  • 1
    I would say a) you don't need to know b) it could change at any time so you won't want to base a solution on a particular implementation. In C++ once you have compiled the code you know it won't change, in Java that is not the case, it even changes while the program is running. – Peter Lawrey Apr 19 '12 at 10:06
  • @PeterLawrey What do you mean by inlined? – Govinda Sakhare Sep 14 '20 at 13:14
4

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.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • Sure. I think the resolution, at runtime, would use `Class` file of B and load method from there, that's why B.f() is called – Bober02 Apr 19 '12 at 10:34
  • @Bober02 both this answer and your comment are only speculations regarding the second question, you didn't mention exactly how B's implementation of f() gets selected. – Sajuuk May 13 '19 at 08:16
  • @Sajuuk vtable, virtual method table, is a term of the _implementation_ in C++, and not only that language. Java _specifies_ going over the classes _this_. Java does not prescribe nor describe an implementation. See https://www.quora.com/How-is-the-virtual-method-table-implemented-in-Java – Joop Eggen May 13 '19 at 08:37
  • @JoopEggen are you suggesting that different Java implementations share almost nothing in common when it comes to implement vtable? or it is pointless discussing vtable implementation when no specific implementation is mentioned? – Sajuuk May 13 '19 at 11:22
3

Here is a related question with answers containing proper links. One thing to note, that was not articulated in the SO question I reference, is that all methods in Java are implicitly virtual. If curious about the technical details, have a look at the jvm.h (search for the string "vtable").

Community
  • 1
  • 1
Alexander Pavlov
  • 31,598
  • 5
  • 67
  • 93