wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes What are virtual functions ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What are virtual functions ?" Watch "What are virtual functions ?" New topic
Author

What are virtual functions ?

sandeep kokane
Greenhorn

Joined: Nov 29, 2000
Posts: 19
What are virtual functions ? and Can we call Virtual funciton in a constructor ?
Wilfried LAURENT
Ranch Hand

Joined: Jul 13, 2001
Posts: 269
This is a Java(TM) forum, not a C++ forum.
You may want to talk of interfaces...
W.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
In short, "virtual function" is the C++ term for a polymorphic function/method.
In Java, all (non-static) methods are polymorphic - there is no way to declare a "non-virtual function".
[ July 11, 2002: Message edited by: Ilja Preuss ]

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
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
Sorry Ilja, but declaring a method as "final" in Java is roughly equivalent to a non-virtual in C++.
It's a horrible thing to do though.


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Declaring a function "private" also makes it effectively non-virtual.


"I'm not back." - Bill Harding, Twister
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Frank Carver:
Sorry Ilja, but declaring a method as "final" in Java is roughly equivalent to a non-virtual in C++.
It's a horrible thing to do though.

Yeah, *very* roughly...
Mhh, can you declare a method as non-virtual which overrides a virtual one in C++? What would happen?
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Jim Yingst:
Declaring a function "private" also makes it effectively non-virtual.

Yes, you are right, of course.
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Hi sandeep,
The virtual modifier signals the compiler that this member function is expected to be overridden by a derived class. See the sample code below:

The member function invoked depends on the runtime type of the object being referenced by the pointer. Note this polymorphic behavior only applies to pointers and references.
sandeep kokane
Greenhorn

Joined: Nov 29, 2000
Posts: 19
Thanks A Lot all of u. Actually i know that there is not "virtual" keyword in the JAVA. But the question about is asked me in the JAVA technical interview. So i was confuse that time. Thanks A lot again .
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What are virtual functions ?