-1

I am currently working on my programming language and it will have only interfaces and no object inheritance. And I have an issue, because I don't know how to make a vtable so an appropriate method could be called in a class with many interfaces inherited.

In Java, you can write like this

interface A {
    void a();
}

interface B {
    void b();
}

interface C {
    void c();
}

class Something1 implements A, B{
    ... implementing a() and b() ...
}

class Something2 implements B, C{
    ... implementing b() and c() ...
}

...

B obj = new Something1();
obj.b();

obj = new Something2();
obj.b();

And the JVM will call b() method with the only knowledge that the object inherits B interface (if not optimized)

So I think, firstly JVM will go to the vtable of the object and then ... How can it find b() in the vtable and what kind of vtable must be generated.

P.S The question is not about Java at all. It could be an example with C++ or PHP, it doesn't matter. It's just simplier to write an example in Java

Arthur
  • 23
  • 2
  • since this is your own language, you *might* do something like ` Z obj = new Something1();` your `Something2` assignment to `obj` is more troubling, because it implies a change in type. – Elliott Frisch Jul 08 '18 at 05:44
  • The language very much matters, as there are huge differences between statically compilef languages and dynamic ones here. – GhostCat Jul 08 '18 at 06:45
  • Beyond that: you have to understand that the "left hand type" used in the source code on the left side of the assignment doesn't matter at runtime. At runtime, when an object is instantiated, its exact class is known. Including full knowledge of all methods belonging to that class. At runtime, all these facts are known, and not going to change for a specific object! – GhostCat Jul 08 '18 at 06:48
  • Also note that just using a search engine could have given you various other questions with good answers, here and on other places. Doing that research is an essential part of such exercises. You should avoid asking "explain it to me", rather find your own approach and then talk to your peers to get feedback. – GhostCat Jul 08 '18 at 06:53
  • My language will be statically typizied and compiled with very broad usage of type inference. And the assignment is just an example of the exact situation where you cast an object to an interface and then find out where some method is. I could search for some better solutions and do some more research on some better approaches if there wasn't a lack of time because this language should be done in 10 or 9 days from now. I'm not asking for "explanation". I'm just asking for some reference or approach so I could do some research and make it by myself. – Arthur Jul 08 '18 at 21:29

1 Answers1

-2

VTables with multiple inheritance are normally implemented via a vtable dispatcher. Which means you create per inherited interface a vtable and pass according to the used interface the matching vtable.

In your case you would create two vtables for each class. One matching the B interface and one matching the A or C interface. When passing your class around via the B interface. You pass your class data-pointer + the pointer to the B interface vtable.

user6556709
  • 1,272
  • 8
  • 13
  • No you wouldn't. You would provide a composite vtable for the class and adjust the pointer during the call such that the correct part of the vtable would be accessed, just like in subclassing. You would not provide an extra runtime parameter for what is already known at compile time. – user207421 Jul 08 '18 at 06:47