views:

2235

answers:

6

How are the virtual functions work in C# and Java ? Does it use same vtable and vpointer concept similar to C++ or is it something totally different?

+1  A: 

I am sure Java does not use vtables, since it is able to support binary compatibility.

To Clarify: The vtable is built when the derived/sub class is compiled. If the base/super class layout changes then the vtable for the d/s needs to be recompiled. In Java this is not the case. You can change the b/s without having to recompile the d/s.

Hmmm. It is possible that a vtable is built at runt-time. when the class is loaded. In which case Java and C++ would be same same but different.

Hemal Pandya
Not sure how a vtable (i.e. a table of function pointers) that is used at runtime preclude binary compatibility?
Andreas Huber
I was thinking that since the layout of the base object can change the lookup has to go through an extra level of indirection.
Hemal Pandya
actually presence of vtable depends on concrete implementation of runtime
aku
+2  A: 

Yes, they share the same concept.

Here is a link to description of method calls in .NET

Article about virtual method tables on Wikipedia

aku
A: 

As far as I know they do use the same concept.

Miles D
+2  A: 

There is no virtual keyword in Java at least.

It just resolves to the most derived version of whatever method you're calling...

class A{
void sayhi(){ System.out.println("A");}
}
class B extends A{
void sayhi(){ System.out.println("B");}
}

A a = new B();
a.sayhi();

Will print "B".

You can create "pure virtual" methods by declaring a class Abstract and leaving the pure virtual methods declared but unimplemented. Or by using interface / implements instead of class / extends. An interface is basically a class where all of the methods are pure virtual. This has the added bonus that a class can implement multiple interfaces, since unlike C++ a Java class can only inherit one other class directly.

EDIT:


In response to your comment, Naveen:

If you said A a = new A(); a.sayhi(); it would print "A".

The java terminology is dynamic. You can think of it as virtual, but that may confuse some Java devs. the ones who don't know C++, at least. In Java there are no explicit pointers, so we don't need to worry about virtual / non virtual. There are no VTables, you just backtrack the class and its ancestors until you find an implementation of the method you want. There's only single inheritance, so you don't have to worry about order of constructors (it's always bottom up).

In C++ you get different behaviour if you have virtual methods and do something like

a->sayhi();

where a was A* pointing to an instance of B instead of

a.sayhi();

where a was an object of type A holding an object of type B

patros
I know that there is no virtual keyword in Java, but isn't everything virtual there by default? BTW, in the above example what happens ifA a = new A(); a.sayhi(); In this case how can we determine in runtime which function to call?
Naveen
Why would you expect something else the A.sayhi() being called? You constructed an object of type A, so of course A.sayhi() is executed and “A” is printed.
Bombe
Thanks for the explanation, I got how it is working in Java now.
Naveen
A: 

All languages supporting polymorphism use vtables to resolve method calls to the correct function. So also Java and .NET.

They both compile to some intermediate langauge (IL for .NET and byte code for java) but the vtable is not visible in this intermediate language. It is supported by the underlying engine (CLR for .NET)

A: 

Pure virtual functions are in C++. C# uses Interfaces to serve the same purpose, so I'd suggest you go that route.