Virtual function
From Free net encyclopedia
In many object-oriented programming languages (such as [[C++]], C#, D and VB.NET), a virtual function or virtual method is a function that when overridden by a subclass will be used by the base class. Normally, when a function is overridden, only the subclass will use the overidden method; the base will continue to use the original. Other object-oriented languages such as Java have the same concept with different terminology.
For example, a base class Animal
could have a virtual function eat
. Subclass Fish
would implement eat
differently than subclass Wolf
, but you can invoke eat
on any base class instance, and get the behavior of the specific subclass.
This allows a programmer to process a list of objects of class Animal
, telling each in turn to eat (by calling eat
), with no knowledge of what kind of animal may be in the list. You also do not need to have knowledge of how each animal eats, or what the complete set of possible animal types might be.
Abstract Classes and Pure Virtual Functions
A pure virtual function or pure virtual method is a virtual function that has a declaration (signature), but no definition (implementation). This may be used where it does not make sense to provide a default implementation of a method, or where the method declarations are being used to define an interface for which other classes will supply all implementations.
In [[C++]], virtual functions can be explicitly marked as having no implementation by using a special syntax (example: class B{virtual void apurevirtualfunction() = 0;}
), and are then called pure virtual functions. The function prototype then serves as a stub that provides only the signature of the eventual implementing method. The advantage of abstracting a function in this manner is that you can write code to call it without knowing how the function will be implemented; instead, you only need to know that the module or the abstract class needs a certain functionality from the object on which it operates. The actual definition or definitions will be provided later as overloaded functions in derived classes. (The compiler knows which method implementation to call at runtime by creating a list of pointers to all the virtual functions, called the vtable
or virtual table.)
A real-world example might be a system of Account classes. Various pieces of code may need to calculate the interest for an account, but the actual calculation will depend on what kind of account is involved. The designer may deem it undesirable to provide a default implementation of a "Calculate Interest" method, instead leaving the actual implementation to the designers of the derived classes "Current Account" and "Savings Account." In this case, the calculateInterest()
method would be declared in the Account class as a pure virtual function.
The presence of pure virtual functions implies that the class containing them is an abstract class, and cannot be instantiated. Only the subclasses of the class, which provide the actual implementations, can be instantiated. If it were permitted to create an instance of a class with pure virtual functions, there would be no code to execute when the virtual function is invoked on the object at runtime. Thus, in C++, pure virtual functions force derived classes to implement a piece of functionality to fulfill the pure virtual function.
An abstract class that contains only pure virtual functions, and not any data members or ordinary methods, can serve to define an interface. An interface class might also contain enumeration constants or nested class definitions. C++ classes can be used to express the interface concept because C++ supports multiple inheritance. Classes could not be used to provide interface functionality in Java, because a Java class can have only one superclass, while a class can implement any number of interfaces in addition to having a base class. For this reason as well as clarity of design, Java has a separate interface concept.