Introduction
Method overloading, method overriding and method hiding are very beautiful features in C# but at the same time they are very confusing for beginners. This article is for beginners to understand these concepts.
Methods Overloading
The method overloading feature in C# is very helpful in code reusability by creating a different version of a method, meaning method overloading is nothing but a technique to create two or more methods of the same name but different signatures.
Same name but different signatures
Method / Function overloading is multiple methods in a class with the same name but different signatures.
The following is a C# method:
- public static void myMethod(int a, int b)
- {
- Console.WriteLine("myMethod Version 1 is printed");
- }
- // Number of parameter
- public static void myMethod(int a, int b, int c)
- {
- Console.WriteLine("myMethod Version 2 is printed");
- }
- // Type of parameters
- public static void myMethod(float a, float b, float c)
- {
- Console.WriteLine("myMethod Version 3 is printed");
- }
- public static void myMethod(float a, int b, float c)
- {
- Console.WriteLine("myMethod Version 4 is printed");
- }
- // Method parameter
- public static void myMethod(float a, out int b, float c)
- {
- Console.WriteLine("b={0} , myMethod Version 5 is printed");
- b =(int)(a + c);
- }

- namespace MethodOverloading
- {
- class Program
- {
- static void Main(string[] args)
- {
- myMethod(4,4);
- }
- public static void myMethod(int a, int b)
- {
- Console.WriteLine("myMethod Version 1 is printed");
- }
- // Number of parameter
- public static void myMethod(int a, int b, int c)
- {
- Console.WriteLine("myMethod Version 2 is printed");
- }
- // Type of parameters
- public static void myMethod(float a, float b, float c)
- {
- Console.WriteLine("myMethod Version 3 is printed");
- }
- public static void myMethod(float a, int b, float c)
- {
- Console.WriteLine("myMethod Version 4 is printed");
- }
- // Method parameter
- public static void myMethod(float a, out int b, float c)
- {
- Console.WriteLine("b={0} , myMethod Version 5 is printed");
- b =(int)(a + c);
- }
- }
Method overriding is nothing but a feature by which a derived class can provide a new implementation for the base class method having the same name and signature as in a derived class. A derived class method can be overridden using the keyword override and that must be virtual or abstract in the base class.
Base Class
- public class cars
- {
- public virtual void displayBrand()
- {
- Console.WriteLine("Base Class - I am Cars");
- }
- }
Derived Class
- public class Maruti : cars
- {
- public override void displayBrand()
- {
- Console.WriteLine("Derived Class - I am Maruti");
- }
- }
Method overriding can happen in a derived class. That only means that this feature can be used whenever we have a requirement for the method with the same name and signature but a different implementation in the derived class.
Sample Code
- namespace MethodOverriding
- {
- class Program
- {
- public class cars
- {
- public virtual void displayBrand()
- {
- Console.WriteLine("Base Class - I am Cars");
- }
- }
- public class Maruti : cars
- {
- public override void displayBrand()
- {
- Console.WriteLine("Derived Class - I am Maruti");
- }
- }
- static void Main(string[] args)
- {
- cars car = new Maruti();
- car.displayBrand();
- Console.ReadLine();
- }
- }
This can be done by the new keyword in the derived class method implementation, in other words the derived class has the same method with the same name and signature.
Base Class
- public class cars
- {
- public virtual void displayBrand()
- {
- Console.WriteLine("Base Class - I am Cars");
- }
- }
- public class Honda : cars
- {
- public new void displayBrand()
- {
- Console.WriteLine("Derived Class - I am Honda");
- }
- }
But the new keyword will help to invoke the base class version of the method displayBrand(); this is called method hiding.
- cars car = new Maruti();
- car.displayBrand();
Sample Code
- namespace MethodHiding
- {
- class Program
- {
- public class cars
- {
- public virtual void displayBrand()
- {
- Console.WriteLine("Base Class - I am Cars");
- }
- }
- public class Honda : cars
- {
- public new void displayBrand()
- {
- Console.WriteLine("Derived Class - I am Honda");
- }
- }
- static void Main(string[] args)
- {
- cars car = new Maruti();
- car.displayBrand();
- Console.ReadLine();
- }
- }
Important Sticky
- In method overloading, the method with the name and signature but different return type will provide a complier error and is not allowed in C#.
- Method overriding is only possible in derived classes.
- To override a base class method in a derived class with the same name and signature, the base class method should be virtual or abstract.
- Method hiding will be done using the new keyword in the derived class for virtual methods in the base class.

Akhil MittalPosted Jul 23, 2014, 12:47 AM
"The method overloading feature in C# is very helpful in code reusability by creating a different version of a method". How reusability is achieved using method overloading?