top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration

Facebook Login
Site Registration

What is overloading and method hiding in C#?

0 votes
43 views
What is overloading and method hiding in C#?
posted Apr 7, 2017 by Rohini Agarwal

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

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");  
}  

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.

// 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 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

public class cars  
{  
public virtual void displayBrand()  
{  
Console.WriteLine("Base Class - I am Cars");  
}  
}  

Derived Class

public class Honda : cars  
{  
public new void displayBrand()  
{  
Console.WriteLine("Derived Class - I am Honda");  
}  
}  

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.

cars car = new Maruti();  
car.displayBrand();  
answer Apr 17, 2017 by Shweta Singh
Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...