Differences Between Virtual, Overriding, and New Keyword In C#

"Virtual" and "Overriding" keywords are used for method overriding, a concept of Polymorphism while the "New" keyword is used for method hiding.

Now, let's check how to use these keywords in C#.

Example 1

  1. namespace ConsoleProg {  
  2.     class A {  
  3.         public void show() {  
  4.             Console.WriteLine("A");  
  5.         }  
  6.     }  
  7.     class B: A {  
  8.         public void show() {  
  9.             Console.WriteLine("B");  
  10.         }  
  11.     }  
  12.     class C: B {  
  13.         public void show() {  
  14.             Console.WriteLine("C");  
  15.         }  
  16.     }  
  17.     class Program {  
  18.         static void Main() {  
  19.             A a = new A();  
  20.             a.show(); // print-- A  
  21.             B b = new B();  
  22.             b.show(); // print-- B  
  23.             C c = new C();  
  24.             c.show(); // print-- C  
  25.             A a1 = new B();  
  26.             a1.show(); // print-- A  
  27.         }  
  28.     }  
  29. }  
Note -  The above code shows some warning messages.

 

  1. 'ConsoleProg.B.show()' hides inherited member 'ConsoleProg.A.show()'. Use the new keyword if hiding was intended.
  2. ‘ConsoleProg.C.show()' hides inherited member 'ConsoleProg.B.show()'. Use the new keyword if hiding was intended.

Solution of the above warning is to use either "New" keyword or "Overriding" keyword in derived class in method.

Virtual and Overriding keywords (Method overriding)

If you want method overriding in the derived class, then the base class method must have the "Virtual" keyword otherwise it will throw an error.

Example 2

  1. namespace ConsoleProg {  
  2.     class A {  
  3.         public virtual void show() {  
  4.             Console.WriteLine("A");  
  5.         }  
  6.     }  
  7.     class B: A {  
  8.         public override void show() {  
  9.             Console.WriteLine("B");  
  10.         }  
  11.     }  
  12.     class C: B {  
  13.         public override void show() {  
  14.             Console.WriteLine("C");  
  15.         }  
  16.     }  
  17.     class Program {  
  18.         static void Main() {  
  19.             A a = new A();  
  20.             a.show(); // print-- A  
  21.             B b = new B();  
  22.             b.show(); // print-- B  
  23.             C c = new C();  
  24.             c.show(); // print-- C  
  25.             A a1 = new B();  
  26.             a1.show(); // print-- B  
  27.         }  
  28.     }  
  29. }  
New Keyword (Method hiding)

The "New" keyword is used for method hiding in the base class method from derived class.

Example 3
  1. namespace ConsoleProg {  
  2.     class A {  
  3.         public void show() {  
  4.             Console.WriteLine("A");  
  5.         }  
  6.     }  
  7.     class B: A {  
  8.         public new void show() {  
  9.             Console.WriteLine("B");  
  10.         }  
  11.     }  
  12.     class Program {  
  13.         static void Main() {  
  14.             A a = new A();  
  15.             a.show(); // print-- A  
  16.             B b = new B();  
  17.             b.show(); // print-- B  
  18.             A a1 = new B();  
  19.             a1.show(); // print-- A  
  20.         }  
  21.     }  
  22. }  
Method Overriding and Hiding

Example 4
  1. namespace ConsoleProg {  
  2.     class A {  
  3.         public void show() {  
  4.             Console.WriteLine("A");  
  5.         }  
  6.     }  
  7.     class B: A {  
  8.         public new virtual void show() {  
  9.             Console.WriteLine("B");  
  10.         }  
  11.     }  
  12.     class C: B {  
  13.         public override void show() {  
  14.             Console.WriteLine("C");  
  15.         }  
  16.     }  
  17.     class Program {  
  18.         static void Main() {  
  19.             A a = new A();  
  20.             a.show(); // print-- A  
  21.             B b = new B();  
  22.             b.show(); // print-- B  
  23.             C c = new C();  
  24.             c.show(); // print-- C  
  25.             A a1 = new B();  
  26.             a1.show(); // print-- A  
  27.             B b2 = new C();  
  28.             b2.show(); // print-- C  
  29.             A a2 = new C();  
  30.             a2.show(); // print-- A  
  31.         }  
  32.     }  
  33. }  
Example 5
  1. namespace ConsoleProg {  
  2.     class A {  
  3.         public virtual void show() {  
  4.             Console.WriteLine("A");  
  5.         }  
  6.     }  
  7.     class B: A {  
  8.         public override void show() {  
  9.             Console.WriteLine("B");  
  10.         }  
  11.     }  
  12.     class C: B {  
  13.         public override void show() {  
  14.             Console.WriteLine("C");  
  15.         }  
  16.     }  
  17.     class Program {  
  18.         static void Main() {  
  19.             A a = new A();  
  20.             a.show(); // print-- A  
  21.             B b = new B();  
  22.             b.show(); // print-- B  
  23.             C c = new C();  
  24.             c.show(); // print-- C  
  25.             A a1 = new B();  
  26.             a1.show(); // print-- B  
  27.             B b2 = new C();  
  28.             b2.show(); // print-- C  
  29.             A a2 = new C();  
  30.             a2.show(); // print-- C  
  31.         }  
  32.     }  
  33. }  
Example 6
  1. namespace ConsoleProg {  
  2.     class A {  
  3.         public virtual void show() {  
  4.             Console.WriteLine("A");  
  5.         }  
  6.     }  
  7.     class B: A {  
  8.         public override void show() {  
  9.             Console.WriteLine("B");  
  10.         }  
  11.     }  
  12.     class C: B {  
  13.         public new void show() {  
  14.             Console.WriteLine("C");  
  15.         }  
  16.     }  
  17.     class Program {  
  18.         static void Main() {  
  19.             A a = new A();  
  20.             a.show(); // print-- A  
  21.             B b = new B();  
  22.             b.show(); // print-- B  
  23.             C c = new C();  
  24.             c.show(); // print-- C  
  25.             A a1 = new B();  
  26.             a1.show(); // print-- B  
  27.             B b2 = new C();  
  28.             b2.show(); // print-- B  
  29.             A a2 = new C();  
  30.             a2.show(); // print-- B  
  31.         }  
  32.     }  
  33. }  
Important Point

Override

We can use this keyword for polymorphism implementation along with "Virtual" keyword, with the same name and parameters as in base class. It is called "run time polymorphism" or "late binding."

New

The "New" keyword is in polymorphism, with the same name and different parameters. This is also known as "Compile time binding" or "early binding."

Ebook Download
View all
Learn
View all