In our earlier Article, we have discussed about benefits and usage of virtual
modifier in C#. We learnt that virtual
keyword enables to override the members in the derived classes.
In this Article, we will discuss about override
and new
modifiers. These modifiers are used during the inheritance in derived the class which will inherit from it’s base class.
The override
Modifier
This modifier is used to override the behavior of the inherited member from it’s base class. That means, using this we can redefine the member in the derived class. The base class definition will be override in the derived class.
Keep in mind that, we can not override all the members from the base class. The member from the base class must be defined with virtual
, abstract
or override
modifiers in order to override them in the virtual class.
C# doesn’t allow to override a static
member and also doesn’t allow to override non-virtual members.
private
members are local to the class where the private
members are defined. These will not inherit to the derived classes. Hence can not be overridden.
The new
Modifier
To hide the base class members in the derived classes, we use new
modifier. By default, C# compiler hides the base class members in the derived classes. You must have to declare the inherited member with this modifier; if you are not intended to override it. Otherwise, the compiler will throw the Warning message, during the compilation.
Warning 1 ‘Modifiers.AnotherDerivedClass.Display(string)’ hides inherited member ‘Modifiers.BaseClass.Display(string)’. Use the new keyword if hiding was intended.
Let’s take a simple example; where BaseClass
has two methods; named Display
; one will takes String
as an argument another will take an int
as an argument. The BaseClass
has derived class; DerivedClass
inherited these base class methods.
Override the Display
method using override
modifier in DerivedClass
. And define another Display
method to hide it’s base class version in DerivedClass
using new
modifier. That means, now we have 2 Display
methods in DerivedClass
; one is defined using override
modifier and another one is defined using new
modifier.
Keep that in mind that, we can not use both new
and override
modifiers on the same member. If you attempt to use, the compiler will throw the below Error;
Error 1 A member ‘Modifiers.DerivedClass.Display(string)’ marked as override cannot be marked as new or virtual
Let’s create an instance of the derived class of type BaseClass
. And call the derived class’s Display
method using BaseClass
‘s object. The code looks like below:
BaseClass b = new BaseClass();
BaseClass d = new DerivedClass();
b.Display("Hello!");
d.Display("Hi!");
b.Display(100);
d.Display(200);
When calling the Display
method using BaseClass
‘s object; if the method is overridden, it will executes the overridden method. If the method is hidden in derived class, it executes the BaseClass
‘s version of the method.
Putting together, here is the working example.
The result looks like below.
Base Class :Hello! Derived Class :Hi! Base Class :100 Base Class :200
We will discuss more topics as we go.
(Raju)