Methods Overloading Vs Method Overriding Vs Method Hiding in C#

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:

  1. public static void myMethod(int a, int b)  
  2. {  
  3. Console.WriteLine("myMethod Version 1 is printed");  

 

A method signature indicates the number of parameters, types of parameters or kinds of method parameters used for creating an overloaded version of a method. An important point is the return type of the method is not includeed in the signature, meaning if we try to create the method with the same but different return type then we will get a complier error message.
  1. // Number of parameter  
  2.   
  3. public static void myMethod(int a, int b, int c)  
  4. {  
  5. Console.WriteLine("myMethod Version 2 is printed");  
  6.   
  7. }  
  8. // Type of parameters  
  9.   
  10. public static void myMethod(float a, float b, float c)  
  11. {  
  12. Console.WriteLine("myMethod Version 3 is printed");  
  13. }  
  14.   
  15. public static void myMethod(float a, int b, float c)  
  16. {  
  17. Console.WriteLine("myMethod Version 4 is printed");  
  18. }  
  19. // Method parameter  
  20.   
  21. public static void myMethod(float a, out int b, float c)  
  22. {  
  23. Console.WriteLine("b={0} , myMethod Version 5 is printed");  
  24. b =(int)(a + c);  

Here in the following we can see that the 5 versions of various methods are available with the same name but different signatures.


 
Sample Code
  1. namespace MethodOverloading  
  2. {  
  3. class Program  
  4. {  
  5. static void Main(string[] args)  
  6. {  
  7. myMethod(4,4);  
  8. }  
  9.   
  10. public static void myMethod(int a, int b)  
  11. {  
  12. Console.WriteLine("myMethod Version 1 is printed");  
  13. }  
  14. // Number of parameter  
  15.   
  16. public static void myMethod(int a, int b, int c)  
  17. {  
  18. Console.WriteLine("myMethod Version 2 is printed");  
  19. }  
  20. // Type of parameters  
  21.   
  22. public static void myMethod(float a, float b, float c)  
  23. {  
  24. Console.WriteLine("myMethod Version 3 is printed");  
  25. }  
  26.   
  27. public static void myMethod(float a, int b, float c)  
  28. {  
  29. Console.WriteLine("myMethod Version 4 is printed");  
  30. }  
  31. // Method parameter  
  32.   
  33. public static void myMethod(float a, out int b, float c)  
  34. {  
  35. Console.WriteLine("b={0} , myMethod Version 5 is printed");  
  36. b =(int)(a + c);  
  37. }  
  38.   

Method Overriding

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

  1. public class cars  
  2. {  
  3. public virtual void displayBrand()  
  4. {  
  5. Console.WriteLine("Base Class - I am Cars");  
  6. }  

Derived Class

  1. public class Maruti : cars  
  2. {  
  3. public override void displayBrand()  
  4. {  
  5. Console.WriteLine("Derived Class - I am Maruti");  
  6. }  

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

  1. namespace MethodOverriding  
  2. {  
  3. class Program  
  4. {  
  5. public class cars  
  6. {  
  7. public virtual void displayBrand()  
  8. {  
  9. Console.WriteLine("Base Class - I am Cars");  
  10. }  
  11. }  
  12.   
  13. public class Maruti : cars  
  14. {  
  15. public override void displayBrand()  
  16. {  
  17. Console.WriteLine("Derived Class - I am Maruti");  
  18. }  
  19. }  
  20. static void Main(string[] args)  
  21. {  
  22. cars car = new Maruti();  
  23. car.displayBrand();  
  24.   
  25. Console.ReadLine();  
  26. }  
  27.   

Method Hiding
Method hiding is nothing but invoking the hidden base class method when the base class variable reference is pointing to the derived class object.
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

  1. public class cars  
  2. {  
  3. public virtual void displayBrand()  
  4. {  
  5. Console.WriteLine("Base Class - I am Cars");  
  6. }  

Derived Class
  1. public class Honda : cars  
  2. {  
  3. public new void displayBrand()  
  4. {  
  5. Console.WriteLine("Derived Class - I am Honda");  
  6. }  

Here we can see the base class and derived class with the same displayBrand() method name and signature. So whenever a base class variable is pointing to a derived class and the method is called in a normal scenario, it will decide the method call from the derived class at runtime.

But the new keyword will help to invoke the base class version of the method displayBrand(); this is called method hiding.
  1. cars car = new Maruti();  
  2. car.displayBrand(); 

Sample Code

  1. namespace MethodHiding  
  2.   
  3. {  
  4. class Program  
  5. {  
  6. public class cars  
  7. {  
  8. public virtual void displayBrand()  
  9. {  
  10. Console.WriteLine("Base Class - I am Cars");  
  11. }  
  12. }  
  13.   
  14. public class Honda : cars  
  15. {  
  16. public new void displayBrand()  
  17. {  
  18. Console.WriteLine("Derived Class - I am Honda");  
  19. }  
  20. }  
  21.   
  22. static void Main(string[] args)  
  23. {  
  24. cars car = new Maruti();  
  25. car.displayBrand();  
  26.   
  27. Console.ReadLine();  
  28. }  

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.

Up Next
    Ebook Download
    View all
    Learn
    View all