Method Overriding
Method overriding is an important feature of OOP that allows us to re-write a base class function or method with a different definition. Overriding is also known as “Dynamic polymorphism” because overriding is resolved at runtime. Here the signature of the method or function must be the same. In other words both methods (base class method and child class method) have the same name, same number and same type of parameter in the same order with the same return type. The overridden base method must be virtual, abstract or override.
Example
public class BaseClass
{
public virtual string GetMethodOwnerName()
{
return “Base Class”;
}
}
public class ChildClass : BaseClass
{
public override string GetMethodOwnerName()
{
return “Child Class”;
}
}
A method cannot be overriden if:
- Methods have a different return type
- Methods have a different access modifier
- Methods have a different parameter type or order
- Methods are non virtual or static
Shadowing (method hiding)
A method or function of the base class is available to the child (derived) class without the use of the "overriding" keyword. The compiler hides the function or method of the base class. This concept is known as shadowing or method hiding. In the shadowing or method hiding, the child (derived) class has its own version of the function, the same function is also available in the base class.
Example
Public class BaseClass
{
public string GetMethodOwnerName()
{
return "Base Class";
}
}
public class ChildClass : BaseClass
{
public new string GetMethodOwnerName()
{
return "ChildClass";
}
}
Test Code
static void Main(string[] args)
{
ChildClass c = new ChildClass();
Console.WriteLine(c.GetMethodOwnerName());
}
Output

If we do not use the new keyword the compiler generates the warning:

Mixing Method (Overriding and shadowing (Method Hiding))
We can also use shadowing and method overriding together using the virtual and new keywords. This is useful when we want to further override a method of the child (derived) class.
Example
public class BaseClass
{
public virtual string GetMethodOwnerName()
{
return "Base Class";
}
}
public class ChildClass : BaseClass
{
public new virtual string GetMethodOwnerName()
{
return "ChildClass";
}
}
public class SecondChild : ChildClass
{
public override virtual string GetMethodOwnerName()
{
return "Second level Child";
}
}




Sonali GuptaPosted Dec 2, 2024, 10:05 AM
Could you explain , in Overriding and shadowing example both are having exact same line but output is different. Can you explain the execution.
Pankajkumar PatelPosted Sep 5, 2019, 12:19 AM
Good one ...
Ramzanali MominPosted Oct 28, 2018, 11:27 PM
Good Article
Prashant ShrivastavaPosted Jul 4, 2017, 10:29 PM
Are you sure about the last point? A method cannot be overriden if: Methods have a different return type Methods have a different access modifier Methods have a different parameter type or order Methods are non virtual or static
sushil kumarPosted Jun 29, 2015, 8:20 AM
Check also in aspmaterials.blogspot.com
sushil kumarPosted Jun 29, 2015, 8:19 AM
Nice Article..
junaidali syedPosted Jun 11, 2015, 3:04 AM
Nice one :)
Satadal ChatterjeePosted Feb 5, 2015, 5:29 AM
Nice one Jignesh
Raees KhanPosted Jan 30, 2015, 6:25 AM
public override virtual string GetMethodOwnerName() this line is correct ?
Saravanakumar SekaranPosted Dec 3, 2014, 9:34 PM
nice explanation.
Sourabh MishraPosted Jun 2, 2014, 1:30 AM
Nice Article :)