88Pro Thinking

cat /senthoor/mind | grep thought > blog

Wednesday, October 27, 2004

Virtual Function

There was an interview question asking “What’s Java's answer for Virtual Function?”. In order to answer that you got to know what is a Virtual Function. Check out the C++ Below code.

C++ Code


class Base
{
public:
void show()
{ cout << "\nBase";}
};

class Derv1 : public Base
{
public:
void show()
{ cout << "\nDerv1";}
};

class Derv2 : public Base
{
public:
void show()
{ cout << "\nDerv2";}
};

void main()
{
Derv1 dv1;
Derv2 dv2;
Base* ptr;

ptr = &dv1;
ptr->show();

ptr = &dv2;
prt->show();
}



Above C++ case has a base class named Base and two derived classes Derv1 and Derv2 both overriding the show() method. What will be the output if you execute this code? First ptr is being assigned with Derv1 refference and then later with Derv2 refference. After each assignment the show() method is executed. However the output will be,

Base
Base

Why is that? Becuase the C++ compiler ignores the contents (Object Type) of the pointer ptr and chooses the member function that matches the type of the pointer (Refference Type). The rule in C++ is that the compiler selects the function based on the contents of the pointer ptr, not on the type of the pointer.

So if you want to achieve the polymopic effect(for your program to print Derv1 and Derv2) what you should do? You have to declare the draw method in the base class(Base) virtual.

public:
virtual void show()
{ cout << "\nBase";}
};

Java Code


class Base{


public void draw(){
System.out.println("Drawing Base");
}
}
 

class A extends Base{
public void draw(){
System.out.println("Drawing A");
}

}
 
class B extends Base{
public void draw(){

System.out.println("Drawing B");
}
}
 
public class JavaVirt{
public static void main (String args[]){

Base baseA = new A();
Base baseB = new B();
baseA.draw();
baseB.draw();
}
}



Whats the results?

Drawing A
Drawing B

Why? Because in Java all the methods are virtual by default. This link summarizes the difference between Java and C++ when it comes to virtual methods, elegantly.

"When you design a class in C++, you have to decide which functions could possibly be overriden by derived classes, and declare these functions as virtual.

In Java, it's the other way around.

KEY POINT Member functions are "virtual" by default, and it's only if you explicitly want them not to be overriden that you include a special keyword (final)." Extracted from Java Learning Centre.


If you think the story ends there, you are highly mistaken. C# inherits behavior of C++ when it comes to virtual functions. Here are some interesting reading on these two different schools of thought.


Anders Hejlsberg’s Thoughts on Virtual Being Default

Scott Meyers thoughts on Virtual and Non-Virtual functions

C# developer thoughts on Virtual By Default

Check the content under “Binary Compatibility” in this Interview with James Gosling

posted by 88Pro / Wednesday, October 27, 2004

This page is powered by Blogger. Isn't yours?

doteasy.com - free web hosting. Free hosting with no banners.