Virtual function |
In many object-oriented programming languages (such as C plus plus, C Sharp, and Visual Basic .NET), a virtual Functional programming or virtual method (computer science) 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 programming language have the same concept with different terminology.
For example, a base class Animal could have a virtual function eat. Subclass Fly 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), Polymorphism (computer science) 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 (computer science), or where the method declarations are being used to define an interface (computer science) for which other classes will supply all implementations.
In .)
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 (computer science). 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 programming language, 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.
=See also=
|
|