Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What are virtual functions ?

 
sandeep kokane
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are virtual functions ? and Can we call Virtual funciton in a constructor ?
 
Wilfried LAURENT
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a Java(TM) forum, not a C++ forum.
You may want to talk of interfaces...
W.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Declaring a function "private" also makes it effectively non-virtual.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic