Consider following class Hierarchy:

In this example, we will consider three classes which are TestA, TestB and TestC.
namespace OOPS_Concept { class TestA { public void display() { Console.WriteLine("TestA - display()"); } } class TestB : TestA { public void display() { Console.WriteLine("TestB - display()"); } } class Test { static void Main(string[] args) { TestA a; TestB b; a = new TestA(); b = new TestB(); a.display(); // TestA - display() b.display(); // TestB - display() a = new TestB(); a.display(); // TestA - display() Console.Read(); } } }
output :
TestA – display()
TestB – display()
TestA – display()
Program will compile and run successfully but
- The Problem of above code is that, third output should be “TestB –display()” because the variable a have the object of B.
- …Test.cs(15,21): warning CS0108: ‘OOPS_Concept.TestB.display()’ hides inherited member ‘OOPS_Concept.TestA.display()’. Use the new keyword if hiding was intended.
Lets resolve the problem 1.
Keyword Virtual and Override
In C#, if you like to override the parent class method then you must mark the parent method by keyword “Virtual“ and method in derived class which intended to override the parent method should be marked by keyword “override“
Note:
- If parent method is marked by keyword “Virtual” but child is not marked by keyword “override”, then program will compile but the parent method will not be overrided.
- If Child method is marked by keyword “override” but parent method is not marked by keyword “virtual” then program will not compile. It will give following error :
‘OOPS_Concept.TestB.display()’: cannot override inherited member ‘OOPS_Concept.TestA.display()’ because it is not marked virtual, abstract, or override.
Example with keyword Virtual and override:
namespace OOPS_Concept { class TestA { public virtual void display() { Console.WriteLine("TestA - display()"); } } class TestB : TestA { public override void display() { Console.WriteLine("TestB - display()"); } } class Test { static void Main(string[] args) { TestA a; TestB b; a = new TestA(); b = new TestB(); a.display(); // TestA - display() b.display(); // TestB - display() a = new TestB(); a.display(); // TestA - display() Console.Read(); } } }
output:
TestA – display()
TestB – display()
TestB – display()
Method Hiding – Keyword new:
As discussed earlier, second point in output of first program was “Compiler generated warning message”.
Because c# also support Method Hiding. To mark method as “hiding” use keyword “new” in derived class .
Keyword “new” can be used with keyword “virtual” also.
Quick Note :
If keyword “override” is used in derive class then its override the parent method.
If Keyword “new” is used in derive class then derive method hided by parent method. As shown in below program:
namespace OOPS_Concept { class TestA { public void display() { Console.WriteLine("TestA - display()"); } } class TestB : TestA { public virtual new void display() { Console.WriteLine("TestB - display()"); } //Instead of virtual new, new can also be written. } class TestC : TestB { public new void display() { Console.WriteLine("TestC - display()"); } } class Test { static void Main(string[] args) { TestB b; TestC c; b = new TestB(); b.display(); // TestB - display() b = new TestC(); b.display(); // TestB - display() instead of TestC - display() Console.Read(); } } }
Output:
TestB – display()
TestB – display()
Leave a Reply