Javatpoint Logo
Javatpoint Logo

Difference between Method Overriding and Method Hiding in C#

In this article, we will discuss about the Method Overriding and Method Hiding. But before discussing their differences, we must know about the Method Overriding and Method Hiding.

What is Method Overriding?

Method overriding is an important concept in object-oriented programming (OOP). A derived class offers a customized implementation for a method that has already been defined and implemented in its base class. It enables the derived class to give its version of a method while preserving the method signatures (name, return type, and parameters) from the parent class.

Syntax and Application of Method Overriding:

Method overriding in C# is achieved through the use of the override keyword in the derived class. The derived class's overridden function must have the same signature (name, returning type, and arguments) as the method that is used in the base class.

Example:

Let us take an example to illustrate the use of method overriding in C#.

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 known as method shadowing, occurs when a derived class defines an additional method with the same name and declaration as a method from its base class. However, compared to method overriding, it does not preserve the same method signature or is not involved in polymorphic behavior.

Syntax and Application of Method Hiding:

In C#, method hiding can be done by explicitly minimizing the base class method using the new keyword that was derived in the class. It generates a new method in the class that was derived with the same identity as a method in the base class, but it is not 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

Difference between Method Overriding and Method Hiding in C#

There are several differences between method overriding and method hiding in C#. Some main differences between the method overriding and method hiding are as follows:

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.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA




news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news