my dog learned polymorphism*
The moose likes Java in General and the fly likes What is meant by a 'virtual method'? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "What is meant by a Watch "What is meant by a New topic
Author

What is meant by a 'virtual method'?

Mallesham Karnati
Ranch Hand

Joined: May 11, 2005
Posts: 40
Hi,
I know about static and non-static methods in java. But, what is a 'virtual method' and why is it called so?
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
In C++ and C#, a virtual method is one that is polymorphic. In Java, all non-static method are virtual, all static methods aren't.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Mallesham Karnati
Ranch Hand

Joined: May 11, 2005
Posts: 40
Thanks Ilja for your quick response. Could you please tell me why non-static methods are called 'virtual'? I am not able to figure out the significance of the term 'virtual' in this context.
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
Java doesn't use the virtual keyword directly. C++ and C# do. The Java compiler itself has a virtual mode by which it resolves references at runtime rather than at compile time. Is that what you're referring to?


Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
In many OO languages, polymorphism is implemented by the use of so-called "virtual jump tables". Basically, when a polymorphic method is called, the address of the code to execute is looked up in a table associated with the object. That's why polymorphic methods are also often called "virtual" in those languages.

Does that help?
Mallesham Karnati
Ranch Hand

Joined: May 11, 2005
Posts: 40
Thanks Ilja for your answer. Now I understad it clearly.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is meant by a 'virtual method'?