Virtual, Override and new Keyword in C#

Consider following class Hierarchy:

Multilevel Inheritance
Multilevel Inheritance

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:

  1. 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.
  2. 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()

Posted

in

by

Tags:


Related Posts

Comments

5 responses to “Virtual, Override and new Keyword in C#”

  1. Chancebyluck Avatar
    Chancebyluck

    very good article

  2. Guest Avatar
    Guest

    good wrok

  3. Shekhar Gahlot Avatar

    good work

  4. Suneel Singh Avatar
    Suneel Singh

    very good work………..nice

  5. uday Avatar
    uday

    nice explanation.
    why we need to override?
    when it is used?

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Jitendra Zaa

Subscribe now to keep reading and get access to the full archive.

Continue Reading