In this tutorial, we will explore the concepts of Method Overriding and Method Hiding. However, before delving into their distinctions, it is essential to have a clear understanding of both Method Overriding and Method Hiding.
What is Method Overriding?
Method overriding plays a crucial role in object-oriented programming (OOP). It allows a subclass to provide a specialized implementation for a method that was originally defined and implemented in its superclass. This feature empowers the subclass to introduce its own interpretation of a method while upholding the method signatures (including name, return type, and parameters) inherited from the parent class.
Syntax and Application of Method Overriding:
In C#, method overriding is accomplished by utilizing the override keyword within the subclass. The overridden function in the subclass must precisely match the signature (name, return type, and parameters) of the method defined in the base class.
Example:
Let's consider a scenario to demonstrate the implementation of method overriding in the C# programming language.
using System;
class ParentClass
{
public virtual void print()
{
//display of console statement
Console.WriteLine("This is the parent class method.");
}
}
class Child_Class : ParentClass
{
public override void print()
{
//display of console statement
Console.WriteLine("This is the child class method");
}
}
class Program
{
static void Main(string[] args)
{
//object creation
Child_Class ob = new Child_Class();
ob.print();
}
}
Output:
This is the child class method.
Method Overriding Key Points:
There are several key points of method overriding in C#. Some main key points are as follows:
- Method overriding is related to inheritance because it allows a subclass to give a particular version of a method derived from its superclass.
- The override keyword is applied in the derived class to show that the method has substituted the base class method explicitly.
- Dynamic binding allows for polymorphic behavior by resolving method calls at execution based on the actual type of the object.
What is the Method Hiding?
Method hiding, also referred to as method shadowing, takes place when a subclass introduces a new method with an identical name and declaration as a method in its superclass. Nevertheless, unlike method overriding, it does not maintain the identical method signature or participate in polymorphic behavior.
Syntax and Application of Method Hiding:
In C#, method hiding can be achieved by explicitly overshadowing the base class method using the new keyword within the derived class. This action results in the creation of a new method within the derived class that shares the same name as a method in the base class, but it does not function as an override.
Method Hiding Key Points:
There are several key points of method hiding in C#. Some main key points are as follows:
- Polymorphism: Method hiding does not enable polymorphism because the method call is resolved at compile time according to the reference type, not the actual object type.
- Use of New Keyword: The new keyword is used to clearly indicate that the method in the derived class is concealing the method in the base class rather than overriding it.
- Compile-time binding: The method to be invoked is determined at the time of compilation based on the specified reference type. Hence, it does not show polymorphic behavior.
Key Differences between Method Overriding and Method Hiding
The CSS code snippet below illustrates the styling for a placeholder diagram:
.placeholder-diagram { background: linear-gradient(135deg, #374151 0%, #1f2937 100%); border-radius: 12px; padding: 40px; margin: 20px 0; text-align: center; }
.placeholder-diagram .placeholder-icon { font-size: 3rem; margin-bottom: 10px; }
.placeholder-diagram .placeholder-text { color: #9ca3af; font-size: 1rem; }
There are multiple distinctions between method overriding and method hiding in C#. Some key variances between the two include:
| Features | Method Overriding | Method Hiding |
|---|---|---|
| Basic Functionality | It allows a subclass to come up with a new implementation for a method hat currently exists in its base class. | It invokes a derived class to create a new method with the same signature as a method in its base class, but without necessarily overriding it. |
| Inheritance | It is directly related to inheritance since it happens between a base class and its derived class. | Inheritance is also involved since the derived class expands the functionality of the base class. |
| Polymorphism | Polymorphism is supported, which allows for dynamic method binding at runtime, enabling the method to be generated based on the actual object type. | It does not support polymorphism. The method calls are handled at compile time based on its reference type. |
| Keyword Usage | Invokes the override keyword within the derived class in order to indicate that the base class method is being replaced properly. | In the derived class, the new keyword is used to explicitly reduce the base class method. |
| Method Signature | It maintains a similar method signature (name, return type, arguments) as the method itself in the base class. | It declares an additional method with the same name as the base class method with a different method declaration (if desired). |
| Dynamic Binding | It allows for dynamic method generation based on the actual type of object at runtime, allowing for polymorphic behavior. | Method calls are resolved at build time according to the reference type, resulting in stable binding. |
| Purpose | It is primarily used for modifying or extending the functionality of a base class method. | Typically used for making local changes or adding new functionality without changing the fundamental class method. |
| Explicit Declaration | The base class method has been identified with the virtual keyword, while the class that was derived method is marked with the override keyword. | The new keyword must be used to effectively indicate the purpose of concealing the base class method. |
| Runtime Behaviour | It allows for polymorphic behavior during runtime by resolving method calls according to the actual object type. | Method calls are resolved according to the reference type, which leads to compile-time binding with a lack of polymorphism. |
| Design Patterns | Patterns such as the Template Method Pattern need base class methods to be overridden by subclasses to change functionality. | It is not commonly used for design patterns because of the absence of polymorphism and the possibility for code readability issues. |