Virtual function: Difference between revisions

Content deleted Content added
Show in upper case or pipe?
Line 4:
In [[object-oriented programming]] such as is often used in [[C++]] and [[Object Pascal]], a '''virtual function''' or '''virtual method''' is an inheritable and [[Method overriding (programming)|overridable]] [[function (computer science)|function]] or [[method (computer science)|method]] that is [[dynamic dispatch|dispatched dynamically]]. Virtual functions are an important part of (runtime) [[Polymorphism (computer science)|polymorphism]] in [[object-oriented programming]] (OOP). They allow for the execution of target functions that were not precisely identified at compile time.
 
Most programming languages, such as [[JavaScript]], [[PHP]] and [[Python (programming language)|Python]], treat all methods as virtual by default<ref>{{Cite web|title=Polymorphism (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)|url=https://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html|access-date=2020-07-11|website=docs.oracle.com}}</ref><ref>{{Cite web|title=9. Classes — Python 3.915.25 documentation|url=https://docs.python.org/3/tutorial/classes.html|access-date=20212025-0207-2305|website=docs.python.org}}</ref> and do not provide a modifier to change this behavior. However, some languages provide modifiers to prevent methods from being overridden by derived classes (such as the ''final'' and ''private'' keywords in [[Java (programming language)|Java]]<ref>{{Cite web|title=Writing Final Classes and Methods (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)|url=https://docs.oracle.com/javase/tutorial/java/IandI/final.html|access-date=2020-07-11|website=docs.oracle.com}}</ref> and [[PHP]]<ref>{{Cite web|title=PHP: Final Keyword - Manual|url=https://www.php.net/manual/en/language.oop5.final.php|access-date=2020-07-11|website=www.php.net}}</ref>).
 
== Purpose ==
{{furtherFurther|Dynamic dispatch}}
The concept of the virtual function solves the following problem:
 
Line 87:
*/
static void _Llama_eat(struct Animal *self) {
printf("<Llama at %p> Llamas eat grass!\n", ( void* )(self));
}
 
Line 113:
Although pure virtual methods typically have no implementation in the class that declares them, pure virtual methods in some languages (e.g. C++ and Python) are permitted to contain an implementation in their declaring class, providing fallback or default behaviour that a derived class can delegate to, if appropriate.<ref>[https://en.cppreference.com/w/cpp/language/destructor#Pure_virtual_destructors Pure virtual destructors - cppreference.com]</ref><ref>[https://docs.python.org/3/library/abc.html "abc — Abstract Base Classes: @abc.abstractmethod"]</ref>
 
Pure virtual functions can also be used where the method declarations are being used to define an [[interface (Java)|interface]] - similar to what the interface keyword in Java explicitly specifies. In such a use, derived classes will supply all implementations. In such a [[design pattern]], the abstract class which serves as an interface will contain ''only'' pure virtual functions, but no data members or ordinary methods. In C++, using such purely abstract classes as interfaces works because C++ supports [[multiple inheritance]]. However, because many OOP languages do not support multiple inheritance, they often provide a separate interface mechanism. An example is the [[Java (programming language)|Java programming language]].
 
== Behavior during construction and destruction ==
Line 134:
* [[Virtual class]]
* [[Interface (object oriented programming)]]
* [[Component Object Model|Component object model]] (Microsoft's COM)
* [[Virtual method table]]