"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
- namespace ConsoleProg {
- class A {
- public void show() {
- Console.WriteLine("A");
- }
- }
- class B: A {
- public void show() {
- Console.WriteLine("B");
- }
- }
- class C: B {
- public void show() {
- Console.WriteLine("C");
- }
- }
- class Program {
- static void Main() {
- A a = new A();
- a.show(); // print-- A
- B b = new B();
- b.show(); // print-- B
- C c = new C();
- c.show(); // print-- C
- A a1 = new B();
- a1.show(); // print-- A
- }
- }
- }
- 'ConsoleProg.B.show()' hides inherited member 'ConsoleProg.A.show()'. Use the new keyword if hiding was intended.
- ‘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
- namespace ConsoleProg {
- class A {
- public virtual void show() {
- Console.WriteLine("A");
- }
- }
- class B: A {
- public override void show() {
- Console.WriteLine("B");
- }
- }
- class C: B {
- public override void show() {
- Console.WriteLine("C");
- }
- }
- class Program {
- static void Main() {
- A a = new A();
- a.show(); // print-- A
- B b = new B();
- b.show(); // print-- B
- C c = new C();
- c.show(); // print-- C
- A a1 = new B();
- a1.show(); // print-- B
- }
- }
- }
The "New" keyword is used for method hiding in the base class method from derived class.
Example 3
- namespace ConsoleProg {
- class A {
- public void show() {
- Console.WriteLine("A");
- }
- }
- class B: A {
- public new void show() {
- Console.WriteLine("B");
- }
- }
- class Program {
- static void Main() {
- A a = new A();
- a.show(); // print-- A
- B b = new B();
- b.show(); // print-- B
- A a1 = new B();
- a1.show(); // print-- A
- }
- }
- }
Example 4
- namespace ConsoleProg {
- class A {
- public void show() {
- Console.WriteLine("A");
- }
- }
- class B: A {
- public new virtual void show() {
- Console.WriteLine("B");
- }
- }
- class C: B {
- public override void show() {
- Console.WriteLine("C");
- }
- }
- class Program {
- static void Main() {
- A a = new A();
- a.show(); // print-- A
- B b = new B();
- b.show(); // print-- B
- C c = new C();
- c.show(); // print-- C
- A a1 = new B();
- a1.show(); // print-- A
- B b2 = new C();
- b2.show(); // print-- C
- A a2 = new C();
- a2.show(); // print-- A
- }
- }
- }
- namespace ConsoleProg {
- class A {
- public virtual void show() {
- Console.WriteLine("A");
- }
- }
- class B: A {
- public override void show() {
- Console.WriteLine("B");
- }
- }
- class C: B {
- public override void show() {
- Console.WriteLine("C");
- }
- }
- class Program {
- static void Main() {
- A a = new A();
- a.show(); // print-- A
- B b = new B();
- b.show(); // print-- B
- C c = new C();
- c.show(); // print-- C
- A a1 = new B();
- a1.show(); // print-- B
- B b2 = new C();
- b2.show(); // print-- C
- A a2 = new C();
- a2.show(); // print-- C
- }
- }
- }
- namespace ConsoleProg {
- class A {
- public virtual void show() {
- Console.WriteLine("A");
- }
- }
- class B: A {
- public override void show() {
- Console.WriteLine("B");
- }
- }
- class C: B {
- public new void show() {
- Console.WriteLine("C");
- }
- }
- class Program {
- static void Main() {
- A a = new A();
- a.show(); // print-- A
- B b = new B();
- b.show(); // print-- B
- C c = new C();
- c.show(); // print-- C
- A a1 = new B();
- a1.show(); // print-- B
- B b2 = new C();
- b2.show(); // print-- B
- A a2 = new C();
- a2.show(); // print-- B
- }
- }
- }
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."

Bikesh SrivastavaPosted Jan 25, 2017, 8:34 AM
Very nice article ,well explained.
Mahesh AllePosted Jan 25, 2017, 7:05 AM
Right output for the example 3, 5 and 6 are Example 3 -> A a1 = new B(); // print-- A Example 5 -> A a1 = new B(); // print-- B Example 6 -> A a1 = new B(); // print-- B. Correct the these line in above article.
Raees KhanPosted Jan 25, 2017, 6:52 AM
Check example 3,5 and 6 which are showing wrong output.