Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

What are virtual methods?

 
Ranch Hand
Posts: 94
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers!
From this app, section Flash cards, I got a strange question about "virtual" methods:


True or False: All methods in Java are virtual methods.


And the Answer is


False. Only non-final, non-static and non-private methods are virtual in Java.



But I didn't understand what virtual methods truly are.
And why only certain categories of methods are defineable virtual.
 
author
Posts: 23834
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The term "virtual function" comes from C++. With it, functions support polymorphism, meaning it doesn't matter what pointer type is used, as it will always called the method of the subclass (based on the actual object type, and not the pointer type).

With Java, this control isn't available. Static and private methods are never polymorphic. And the rest are always polymorphic.

Henry
 
Greenhorn
Posts: 3
Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniele Barell wrote:Hi Ranchers!
From this app, section Flash cards, I got a strange question about "virtual" methods:


True or False: All methods in Java are virtual methods.


And the Answer is


False. Only non-final, non-static and non-private methods are virtual in Java.



But I didn't understand what virtual methods truly are.
And why only certain categories of methods are defineable virtual.



It sounds unusual right? But it's not too complicated.

A virtual method is a method whose implementation isn't determined until runtime.

When would this happen?

Overridden methods. You would override a method inherited from a parent class in a child class.

The overridden implementation will be true for every call of the method, even the one it is overriding in
the parent class, and that is assigned to the parent class method call at runtime.



 
Daniele Barell
Ranch Hand
Posts: 94
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Henry, Pani.
Yeah, It's term in Java I've never met before.

But now I can see it has to do with polymorphism and overridden methods.

Thanks a lot!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!