Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code 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
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

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: 23877
142
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!
 
So you made a portal in time and started grabbing people. This tiny ad thinks that's rude:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic