How do 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?
How do 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?
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.
How do virtual functions work in Java?
Coding interviewers love this question. Yes. Although Java does NOT have a virtual keyword, Java has virtual functions and you can write them.
In object-oriented programming, a virtual function or virtual method is a function or method whose behavior can be overridden within an inheriting class by a function with the same signature. This concept is a very important part of the polymorphism portion of object-oriented programming (OOP).
Asking an architecture question about a specific language like this requires great communication skills and a deep mastery of underlying principles of the Java compiler, specifically interfaces, abstract classes, and how inheritance works.
Guide the interviewer onto a specific example of a virtual function.
Yes you can write virtual functions in Java with interfaces.
Java interface methods are all "pure virtual" because they are designed to be overridden. For example:
Yes you can write virtual functions in Java with abstract classes.
Java Abstract classes contain implicitly "virtual" methods, implemented by classes extending it. For Example:
You can force a function to NOT be virtual in a generic class by making it
final
For example:
There is no virtual keyword in Java at least.
It just resolves to the most derived version of whatever method you're calling...
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
where a was A* pointing to an instance of B instead of
where a was an object of type A holding an object of type B
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)
Pure virtual functions are in C++. C# uses Interfaces to serve the same purpose, so I'd suggest you go that route.
In Java virtual function is totally different. In Java, virtual function means a function which can be overridden in its subclasses. So all non-static methods of Java are virtual function. Only the final and private function are not inherited so they are not virtual function.
So we can say that all non-static function which are not final and private are virtual function in Java.