Virtual function
| Polymorphism |
|---|
| Ad hoc polymorphism |
| Parametric polymorphism |
| Subtyping |
In object-oriented programming such as is often used in C++ and Object Pascal, a virtual function or virtual method is an inheritable and overridable function or method that is dispatched dynamically. Virtual functions are an important part of (runtime) polymorphism in object-oriented programming (OOP). They allow for the execution of target functions that were not precisely identified at compile time.
Many programming languages, such as Java, treat all methods as virtual by default. Some of these languages, such as Python, do not allow changing this behavior. However, others may provide modifiers that prevent methods from being overridden by derived classes (such as the final and private keywords in Java and PHP).