C# - Basics - Classes - Inheritance - Virtual Functions
Nisan 10, 2011 by C# Tutorial
|
|
Virtual Functions in C#
This topic contains and tries to cover following subjects:
- Virtual Methods in C#
- Explanation of Virtual Methods in C#
- A basic example of Virtual Mehod and how to use in a Class
Explanation of Virtual methods in C#
Virtual methods-functions enable programmers to override a method in a derived Class. When a derived Class inherits methods and fields of a base Class, it inherits with base Class definition. However, programmer may need to change the bahaviour of inherited method in derived Class. In normal case, if a method is not marked as virtual in base Class, by default, it becomes non-virtual.
For example consider the following scenario.
We have two Class in the scenario. Hardware Class which is sort of main template of all products. Notebook Class which uses main template "Hardware Class" to inherit its hardwareID field, adds its notebook specific feature (for example it has batteryType information field) also. In normal case, "method_displayInfo()" call triggers base Class method. Derived Class has not such method, it had inherited from base. If we add same named method to the derived Class, to display some notebook specific additional information in our Notebook Class method, derived Class method is triggered. However, when you check the compiler, a warning indicates that a base Class method is hided.
namespace NS_hardwareManagerApp
{
//declare a base Class...///
class CL_hardwares
{
public string field_hardwareID;
public void method_displayInfo()
{
Console.WriteLine("Base Class method is triggered...");
Console.WriteLine("ID is: " + field_hardwareID);
}
}
//create a derived Class and inherit base Class...///
class CL_notebooks : CL_hardwares
{
public string field_batteryType;
public void method_displayInfo()
{
Console.WriteLine("Derived Class method is triggered...");
Console.WriteLine("ID is: " + field_hardwareID);
}
}
class CL_x
{
static void Main(string [] args)
{
//create an instance of Class...///
CL_notebooks o_aNotebook = new CL_notebooks();
//assign values to fields of Instance...///
o_aNotebook.field_hardwareID = "1";
//display fields...///
o_aNotebook.method_displayInfo();
Console.ReadLine();
}
}
}
As it is expected, console output indicates that base derived Class method is triggered even Visual Studio warns about hided base Class method.
Virtual methods steps in here, to tell compiler that we are overriding it without hiding base Class method.
Syntax of virtual methods and overriding a derived Class method in C#
To make a base Class method virtual, "virtual" keyword is added to the method after its access modifier (public-private). Further in derived Class, the same named method, "override" keyword is added after the access modifier.
Virtual method syntax example:
namespace NS_hardwareManagerApp
{
//declare a base Class...///
class CL_hardwares
{
public string field_hardwareID;
public virtual void method_displayInfo()
{
Console.WriteLine("Base Class method is triggered...");
Console.WriteLine("ID is: " + field_hardwareID);
}
}
//create a derived Class and inherit base Class...///
class CL_notebooks : CL_hardwares
{
public string field_batteryType;
public override void method_displayInfo()
{
Console.WriteLine("Derived Class method is triggered...");
Console.WriteLine("ID is: " + field_hardwareID);
}
}
class CL_x
{
static void Main(string [] args)
{
//create an instance of Class...///
CL_notebooks o_aNotebook = new CL_notebooks();
//assign values to fields of Instance...///
o_aNotebook.field_hardwareID = "1";
//display fields...///
o_aNotebook.method_displayInfo();
Console.ReadLine();
}
}
}
Another remark is, when we create an instance of Hardware type and initialize as new Notebook: what would happen, and what is the difference?
If you try following initialization, apart from expected, you will notice that derived Class type initialization is calling base Class method.
Why derived Class calls base Class methods when it has its own same named method similar to the preceding working example. It is due to the compile time type and run-time type distinction. When a method is not marked as virtual explicitly, it becomes non-virtual. Marking method virtual tells compiler that initialize that instance with referencing its run time type not the compile-time type. Following image demonstrates it. A notebook type initialization of hardware Class type. In compile type, instance is type of hardware. In run-time it is type of Notebook Class. When it is marked virtual, VS generates run-time type of instance. Otherwise it will not. Therefore, non-virtual methods of instance may not invoke as it is expected. We had not use virtual-override keywords in example provided in image above. Console application seems calling base Class method from notebook type initialization instance.
Data Layers
Area: | programming \ Languages \ csharp \ \ \ |
Ref: | http://msdn.microsoft.com/en-us/library/ms173152.aspx |
Loc: | articles |
Tags: | csharp |
|