Jump to content

Virtual function

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by 161.28.116.39 (talk) at 17:53, 21 November 2003. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

In many object oriented programming languages such as C++, C#, VB.NET and others, a virtual function is a function that can be overridden with specialized implementations in subclasses.

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.

This allows you to process a list of Animals, telling each in turn to eat, with no knowledge of what kind of animal may be in the list. You also need have no knowledge of how each Animal eats.

See also super class