English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

2006-11-12 18:58:29 · 3 answers · asked by muthiah k 1 in Computers & Internet Programming & Design

3 answers

object-oriented programming, a virtual function or virtual method is a function whose behavior, by virtue of being declared "virtual", is determined by the definition of a function with the same signature furthest in the inheritance lineage of the instantiated object on which it is called. This concept is a very important part of the polymorphism portion of object-oriented programming (OOP).


The concept of the virtual function solves the following problem:

In OOP when a derived class inherits from a base class, an object of the derived class may be referred to (or cast) as either being the base class type or the derived class type. If there are base class functions overridden by the derived class, a problem then arises when a derived object has been cast as the base class type. When a derived object is referred to as being of the base's type, the desired function call behavior is ambiguous.

The distinction between virtual and not virtual is provided to solve this issue. If the function in question is designated "virtual" then the derived class's function would be called (if it exists). If it is not virtual, the base class's function would be called.

2006-11-13 18:00:43 · answer #1 · answered by srihari_reddy_s 6 · 0 0

A virtual function in Object-oriented programming is one whose behavior is determined by the nearest definition in the generalization scheme, such as inheritance. So if the Animal class declares a virtual function called speak(), and subclasses (say, Dog, Cow, and Parrot) override this function (provide their own implementation, then calling the function on an object of type Dog will get the Dog implementation (say, "Bow Wow"), while calling the function on a Parrot object will get the Parrot implementation (say, "Polly want a cracker").

In Java, any method which is not declared static, final, or private is virtual by definition, i.e. subclasses can override their runtime behavior. This is a little different than say, C++, where the function must be declared as "virtual".

2006-11-13 12:58:05 · answer #2 · answered by vincentgl 5 · 0 0

In object-oriented programming, a virtual function or virtual method is a function whose behavior, by virtue of being declared "virtual", is determined by the definition of a function with the same signature furthest in the inheritance lineage of the instantiated object on which it is called. This concept is a very important part of the polymorphism portion of object-oriented programming (OOP).

for further information check this http://en.wikipedia.org/wiki/Virtual_functions

2006-11-12 19:47:47 · answer #3 · answered by Hi 1 · 0 0

fedest.com, questions and answers