C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
The virtual keyword designates a method that is overridden in derived classes. We can add derived types without modifying the rest of the program. The runtime type of objects thus determines behavior.
Example. This program introduces two classes, A and B. Class A has a public virtual method called Test. Class B, meanwhile, derives from class A and it provides a public override method called Test as well.
Tip: The virtual modifier tells the compiler that when any class derived from class A is used, an override method should be called.
C# program that introduces virtual method using System; class A { public virtual void Test() { Console.WriteLine("A.Test"); } } class B : A { public override void Test() { Console.WriteLine("B.Test"); } } class Program { static void Main() { // Compile-time type is A. // Runtime type is A as well. A ref1 = new A(); ref1.Test(); // Compile-time type is A. // Runtime type is B. A ref2 = new B(); ref2.Test(); } } Output A.Test B.Test
With virtual methods, the runtime type is always used to determine the best method implementation. In Main, ref1 has a compile-time and runtime type of A. On the other hand, ref2 has a compile-time type of A but a runtime type of B.
Discussion. Why would you need to use virtual methods? Your program may be designed in such a way that you do not know all the types of objects that will occur when it is executed. You can provide a standard (base) type and design around that type.
Then, you can re-implement important functionality depending on the more specific (derived) types. When you call a method on the base type, you invoke the more derived (and useful) method.
Abstract: The term abstract refers to a conceptual type, one that specifies common characteristics but lacks an implementation.
Tip: An abstract class then is used as a template for derived classes that can actually exist.
Private virtual methods cannot be declared in the C# language. In programming languages, polymorphism based on types is a way to virtualize the entry point to a data object. For private methods, you have already entered the data object.
Thus: No virtualized entry point is ever required. And a private virtual method is not useful.
In this example, we see both a program that attempts to use a private virtual method member, as well as the compile-time error that the program triggers. As a reminder, the error occurs during the static compilation phase.
And: This means that no program that declares a private virtual method will ever be executed in any way.
C# program that uses private virtual method class A { private virtual int Test() { return 1; } } class Program { static void Main() { } } Compile-time error Error 1 'A.Test()': virtual or abstract members cannot be private
Virtual methods are an implementation of type-based polymorphism. When you have a derived class and a base class, the derived class can re-implement the base class virtual method. This gives you a dynamic entry point to the class type.
Note: This enables you to clearly separate the usage of the type from the implementation of each subtype.
Whenever a private method is invoked, the type has already been entered. You can always define custom private methods on a type. If you are already inside the type, you can simply call a regular instance private method.
Further: Instance and static methods are faster to invoke than interface and virtual methods.
Notice: Unlike in the C# language, private virtual methods are allowed in the C++ programming language.
And: They are used in some design patterns that emphasize the importance of overriding only certain pieces of an algorithm.
Summary. Virtual specifies that the method can be overridden in derived types. It tells the compiler to generate code that selects an override method. A virtual method can have an implementation. It is called like any other method.