"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();
- B b = new B();
- b.show();
- C c = new C();
- c.show();
- A a1 = new B();
- a1.show();
- }
- }
- }
Note - The above code shows some warning messages.
- '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();
- B b = new B();
- b.show();
- C c = new C();
- c.show();
- A a1 = new B();
- a1.show();
- }
- }
- }
New Keyword (Method hiding)
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();
- B b = new B();
- b.show();
- A a1 = new B();
- a1.show();
- }
- }
- }
Method Overriding and Hiding
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();
- B b = new B();
- b.show();
- C c = new C();
- c.show();
- A a1 = new B();
- a1.show();
- B b2 = new C();
- b2.show();
- A a2 = new C();
- a2.show();
- }
- }
- }
Example 5
- 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();
- B b = new B();
- b.show();
- C c = new C();
- c.show();
- A a1 = new B();
- a1.show();
- B b2 = new C();
- b2.show();
- A a2 = new C();
- a2.show();
- }
- }
- }
Example 6
- 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();
- B b = new B();
- b.show();
- C c = new C();
- c.show();
- A a1 = new B();
- a1.show();
- B b2 = new C();
- b2.show();
- A a2 = new C();
- a2.show();
- }
- }
- }
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."