CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

C# – Modifiers – The virtual Modifier – Why virtual?

Through the series of Articles, we are discussing about Modifiers in C#. In this Article, we will discuss about virtual modifier. virtual modifier is used during the inheritance; deriving classes from it’s base classes.

Why virtual is required?

Consider two functions, with same name and different signature; for example, Display(String msg) and Display(int value). Assume, these two functions will display the passed argument value on the console window. One function deals with string and another function deals with an integer value. If, methods having same name and different type of arguments; we call them overloaded functions.

With overloading functions, it is easy to the compiler to figure out which function to call; depends on the arguments and it’s type. For example,

Display("C#");

Display(100);

It is NOT allowed to define the same function with the same signature in the same class. If you attempt to create, you will see the below Error:

Error 1 Type ‘Modifiers.Program’ already defines a member called ‘Display’ with the same parameter types

But when you deal with Inheritance, where derived class inherited from it’s base class; it is allowed to define the same method as it is in both the classes; and may be the behavior of the methods will be different.

Let’s take a below example; having a BaseClass and it’s DerivedClass. Both these classes have a common method, Display; which will display a message which is passed through it’s argument.

As you aware, we can create instance of DerviedClass of type BaseClass. This is allowed, because of the inheritance. But when you call it’s method Display; which class’s Display method will execute? We think, it’s DerivedClass‘s method. But it is not. C# calls, BaseClass‘s Display method even though we call the method through DerivedClass‘s object.

BaseClass b = new BaseClass();
BaseClass d = new DerivedClass();

b.Display("Hello!");
d.Display("Hi!");

How do we instruct to the compiler to call the right method when calling DerivedClass‘s methods through BaseClass‘s object? Here is the need of virtual; this enables compiler to call the right method.

The virtual Modifier

A virtual keyword is used to define virtual members. By default class members are non-virtual. This modifier is useful to resolve to the correct members during run-time when you use class inheritance.

From above example, the Display method in BaseClass should be defined as a virtual method.

public virtual void Display(String msg)

By defining a method as virtual; C# compiler will resolve to the correct object type and it enables to call the right method. Let’s see whether above example will work now.

During the compilation, the compiler will throw the below warning message; as Display method in DerivedClass is hiding the Display method defined in it’s BaseClass.

Warning 1 ‘Modifiers.DerivedClass.Display(string)’ hides inherited member ‘Modifiers.BaseClass.Display(string)’. Use the new keyword if hiding was intended.

This issue can be resolved, by adding new modifier to the method defined in the DerivedClass. But our purpose is different; the compiler has to call right method when we call the DerivedClass‘s method using BaseClass‘s object. How to instruct this to the compiler? That is where override modifier comes into the picture.

By adding override modifier, the compiler will override the method defined in the BaseClass with the DerivedClass‘s method. So it will call the right method. I will explain new & override modifiers in a separate Article. Just to remember that, new is for method hiding and override is for method overriding.

Here is the complete working example of the code:

We will discuss more topics as we go.

(Raju)

C# – Modifiers – The virtual Modifier – Why virtual?

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top