Virtual methods and classes are useful for implementing inheritance and polymorphism.
There are three basic types of inheritance in Object Oriented Programming:
An abstract overridable routine:
the derived class inherits the routine's interface but NOT it's implementation.
In C# we use the "abstract" keyword or interfaces.
An overridable routine:
the derived class inherits the routine's interface, implementation, and is also allowed to override.
In C# we use the "override" and
"virtual" keywords.
Note: methods are non-virtual by default in C#.
A non-overridable routine:
the derived class inherits the routine's interface, implementation and is NOT allowed to override.
In C# we use the "sealed" keyword.
Note: I'm referring to routines as either a function or procedure.
Functions include: methods, properties, and events in C#.
Procedures are functions that return nothing (void functions).
Simplified:
We obtain polymorphism through inheritance.
We can obtain inheritance and polymorphism through the "virtual" keyword.
Links of interest:
Introduction to inheritance, polymorphism in C#:
http://www.codeproject.com/csharp/csharpintro01.asp
Polymorphism (computer science):
http://en.wikipedia.org/wiki/Polymor...ter_science%29
- Adam Kahtava [
http://adam.kahtava.com]