Method Overloading and Method Overriding in C#

Polymorphism

Polymorphism simply means "many forms" and hence means "having many forms" in C# or for that matter in any Object Oriented Programming language.

Method overloading and method overriding in C# implies one name with multiple functions. In method overloading and method overriding in C# we declare methods with the same name and different parameters in the same class or methods with the same name and same parameters in different classes.

There are two types of method overloading and method overriding in C#. They are the following.

Static Polymorphism

Method overloading and operator overloading are examples. It is also called early binding.

Dynamic Polymorphism

Method overriding and Virtual functions are examples. it is also called late binding.

Example of Method Overloading

  1. class Program {  
  2.     void hello() {  
  3.         Console.WriteLine("HelloIndia");  
  4.     }  
  5.     void hello(string s) {  
  6.         Console.WriteLine(s);  
  7.     }  
  8.     void hello(string s, int marks) {  
  9.         Console.WriteLine(s + " Your marks is " + marks);  
  10.     }  
  11.     public static void Main() {  
  12.         Program p1 = new Program();  
  13.         p1.hello();  
  14.         p1.hello("hello");  
  15.         p1.hello("siddique", 58);  
  16.         Console.ReadLine();  
  17.     } 
Output



So here we saw method overloading.

A method hello() that is declared in the base class A and not redeclared in classes B but B is inherited from A. So see the code and output.
  1. Program   
  2. {  
  3.     class A   
  4.     {  
  5.         public void hello()  
  6.         {  
  7.             Console.WriteLine("helloWorld");  
  8.         }  
  9.     }  
  10.     class B: A {}  
  11.     public static void Main()   
  12.     {  
  13.         A a = new A();  
  14.         a.hello(); // output --> "helloWorld"  
  15.         B b = new B();  
  16.         b.hello(); // output --> "helloWorld"  
  17.         Console.ReadLine();  
  18.     } 
Output



The method hello() can be overridden in classes B.
  1. class Program  
  2. {  
  3.     class A   
  4.     {  
  5.         public void hello()   
  6.         {  
  7.             Console.WriteLine("helloworld A");  
  8.         }  
  9.     }  
  10.     class B: A   
  11.     {  
  12.         public void hello()   
  13.         {  
  14.             Console.WriteLine("helloworld B");  
  15.         }  
  16.     }  
  17.     public static void Main()   
  18.     {  
  19.         A a;  
  20.         B b;  
  21.         a = new A();  
  22.         b = new B();  
  23.         a.hello(); // output --> "helloworld A"  
  24.         b.hello(); // output --> "helloworld B"  
  25.         a = new B();  
  26.         a.hello(); // output --> "helloworld A"  
  27.         Console.ReadLine();  
  28.     } 
output



The output is not really what we, say from Java, expected. The method hello() is a non-virtual method. C# requires the use of the keyword virtual for a method to actually be virtual.

Example
  1. class Program   
  2. {  
  3.     class A   
  4.     {  
  5.         public virtual void hello()   
  6.         {  
  7.             Console.WriteLine("helloworld A");  
  8.         }  
  9.     }  
  10.   
  11.     class B: A   
  12.     {  
  13.         public override void hello()   
  14.         {  
  15.             Console.WriteLine("helloworld B");  
  16.         }  
  17.     }  
  18.   
  19.     public static void Main()   
  20.     {  
  21.         A a;  
  22.         B b;  
  23.   
  24.         a = new A();  
  25.         b = new B();  
  26.         a.hello(); // output --> "helloworld A"  
  27.         b.hello(); // output --> "helloworld B"  
  28.   
  29.         a = new B();  
  30.         a.hello(); // output --> "helloworld B"  
  31.         Console.ReadLine();  
  32.     } 
Output



Method Hiding

C# not only supports method overriding, but also method hiding. Simply put, if a method is not overriding the derived method, it is hiding it. A hiding method must be declared using the new keyword. The correct class definition is as in the following:
  1. class Program   
  2. {  
  3.     class A   
  4.     {  
  5.         public void hello()   
  6.         {  
  7.             Console.WriteLine("helloworld A");  
  8.         }  
  9.     }  
  10.   
  11.     class B: A   
  12.     {  
  13.         public new void hello()   
  14.         {  
  15.             Console.WriteLine("helloworld B");  
  16.         }  
  17.     }  
  18.   
  19.     public static void Main()   
  20.     {  
  21.         A a;  
  22.         B b;  
  23.   
  24.         a = new A();  
  25.         b = new B();  
  26.         a.hello(); // output --> "helloworld A"  
  27.         b.hello(); // output --> "helloworld B"  
  28.   
  29.         a = new B();  
  30.         a.hello(); // output --> "helloworld A"  
  31.         Console.ReadLine();  
  32.     } 
Output



Combining Method Overriding and Hiding

Methods of a derived class can both be virtual and at the same time hide the derived method. In order to declare such a method, both keywords virtual and new must be used in the method declaration as in the following:
  1. class A   
  2. {  
  3.     public void hello() {}  
  4. }  
  5. class B: A   
  6. {  
  7.     public virtual new void hello() {}  
  8. }  
  9.   
  10. class C can now declare a method hello() that either overrides or hides hello() from class B: class C: B {  
  11.     public override void hello() {}  
  12.     // or  
  13.     public new void hello() {}  
  14. }
Conclusion

Only methods in base classes need not override or hide derived methods. All methods in derived classes require to be either defined as new or be overriden.


Recommended Free Ebook
Similar Articles