3. C# Method Hiding Vs Method Overriding Vs Method Overloading
Method Hiding :
C# provides a concept to hide the methods of the base class from derived class, this concept is known as Method Hiding. It is also known as Method Shadowing.
In method hiding, you can hide the implementation of the methods of a base class from the derived class using the new keyword. Or in other words, in method hiding, you can redefine the method of the base class in the derived class by using the new keyword.
// method In base class
public void member()
{
Console.WriteLine("Total number of family members: 3");
}
// Method In derived class
// Reimplement the method of the base class Using new keyword
// It hides the method of the base class
public new void member()
{
Console.WriteLine("Name: Rakesh, Age: 40 \nName: Somya, "+"Age: 39 \nName: Rohan, Age: 20 ");
}
How to call a hidden method?
In method hiding, you can also call the hidden method of the base class in the derived class using three different ways and the ways are:
By using the base
keyword you can call the hidden method of the base class in your derived class
// In derived class
public new void member()
{
// Calling the hidden method of the base class in a derived class
// Using base keyword
base.member();
Console.WriteLine("Name: Rakesh, Age: 40 \nName: Somya,"+" Age: 39 \nName: Rohan, Age: 20");
}
By casting the derived class type to base class type you can invoke the hidden method. As shown in the below example. We know that in inheritance the derived class has all the capabilities of the base class so we can easily cast the object of a derived class into base class type.
// Creating the object of the derived class
My_Member obj = new My_Member();
// Invoking the hidden method
// By type casting
((My_Family)obj).member();
Method Overriding :
Method Overriding is a technique that allows the invoking of functions from another class (base class) in the derived class.
Creating a method in the derived class with the same signature as a method in the base class is called as method overriding.
In simple words, Overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes. When a method in a subclass has the same name, same parameters or signature and same return type(or sub-type) as a method in its super-class, then the method in the subclass is said to override the method in the super-class. Method overriding is one of the ways by which C# achieve Run Time Polymorphism(Dynamic Polymorphism).
In C# we can use 3 types of keywords for Method Overriding:
virtual keyword: This modifier or keyword use within base class method. It is used to modify a method in base class for overridden that particular method in the derived class.
override: This modifier or keyword use with derived class method. It is used to modify a virtual or abstract method into derived class which presents in base class.
base Keyword: This is used to access members of the base class from derived class. It basically used to access constructors and methods or functions of the base class. The base keyword cannot use within a static method. Base keyword specifies which constructor of the base class should be invoked while creating the instances of the derived class.
Use of Base keyword:
1. Call methods or functions of base class from derived class.
2. Call constructor internally of base class at the time of inheritance.
Note:
1. Method overriding is possible only in derived classes. Because a method is overridden in the derived class from the base class.
2. A non-virtual or a static method can’t be overridden.
3. Both the override method and the virtual method must have the same access level modifier.
Method Overloading :
Method Overloading is the common way of implementing polymorphism. It is the ability to redefine a function in more than one form. A user can implement function overloading by defining two or more functions in a class sharing the same name. C# can distinguish the methods with different method signatures. i.e. the methods can have the same name but with different parameters list (i.e. the number of the parameters, order of the parameters, and data types of the parameters) within the same class.It is also known as early binding or comple time polymorphism.
- Overloaded methods are differentiated based on the number and type of the parameters passed as arguments to the methods.
- You can not define more than one method with the same name, Order and the type of the arguments. It would be compiler error.
- The compiler does not consider the return type while differentiating the overloaded method. But you cannot declare two methods with the same signature and different return type. It will throw a compile-time error. If both methods have the same parameter types, but different return type, then it is not possible.
Why do we need Method Overloading ?
If we need to do the same kind of the operation in different ways i.e. for different inputs. In the example described below, we are doing the addition operation for different inputs. It is hard to find many different meaningful names for single action.