Difference between Method hiding and Method Overriding?
Can any one explain me What is difference between Method Hiding and Method Overriding.
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Tuhin PaulPosted Mar 21, 2023, 4:04 PM
Method hiding:
Suppose you have a class Animal that has a method called MakeSound(), which prints a generic sound for an animal. You have a subclass Cat that inherits from Animal and also has a method called MakeSound(), which prints "Meow". If you create an object of the Cat class and call the MakeSound() method on it, the output will be "Meow" instead of the generic animal sound.
Method overriding:
Suppose you have a class Shape that has a method called CalculateArea(), which calculates the area of a generic shape. You have a subclass Circle that inherits from Shape and overrides the CalculateArea() method to calculate the area of a circle. If you create an object of the Circle class and call the CalculateArea() method on it.
Tuhin PaulPosted Mar 16, 2023, 2:31 PM
Example of Method Overriding:
Tuhin PaulPosted Mar 16, 2023, 2:30 PM
Example of Method Hiding
Tuhin PaulPosted Mar 16, 2023, 2:30 PM
Method Hiding is a mechanism in which a subclass declares a static method with the same name and signature as a static method in its superclass. The static method in the subclass "hides" the static method in the superclass, and calls to the method in the subclass will invoke the subclass method, not the superclass method. Method hiding is achieved using the "new" keyword.
Method Overriding is a mechanism in which a subclass provides a specific implementation of a method that is already provided by its superclass. The method in the subclass must have the same name, return type, and parameter list as the method in the superclass. When a method is called on an object of the subclass, the subclass method is invoked, not the superclass method. Method overriding is achieved using the "override" keyword.
Rajeev KumarPosted Mar 16, 2023, 10:36 AM
Whereas in C#, for overriding the base class method in a derived class, you need to declare the base class method as virtual and the derived class method as overriden.
Whereas if a method is overridden then the implementation to be called is based on the run-time type of the argument "this".
Nirav VasoyaPosted Apr 21, 2015, 5:09 AM
Overriding is the definition of multiple possible implementations of the same method signature, such that the implementation is determined by the runtime type of the zeroth argument (generally identified by the name this in C#).
Hiding is the definition of a method in a derived type with a signature identical to that in one of its base types without overriding.
The practical difference between overriding and hiding is as follows:
Hiding is for all other members (static methods , instance members, static members). It is based on the early binding . More clearly , the method or member to be called or used is decided during compile time.
•If a method is overridden, the implementation to call is based on the run-time type of the argument this.•If a method is simply hidden, the implementation to call is based on the compile-time type of the argument this.
Pradeep ShetPosted Feb 12, 2015, 8:11 AM
The main difference between Method hiding and overriding is, when you actually call the method and how you call the method. I will try to explain using example
public class A
{
public virtual void fun(){ //code }
public void fun2(){//Code}
}
public class B : A
{
public override void fun() {/Code/}
public new void fun2(){//Code}
}
Now, the way you call matters in both the case.
if I say
B bobj = new B();
bobj.fun();// this will call inherited class function(i.e - B)
bobj.fun2()// this will again call inherited function(i.e.-B)
But it I say
A bobj = new B();
bobj.fun(); // this will call inherited class function(i.e- B)
bobj.fun2();//This will call base class function (i.e-A)
Because method hiding just hides your method for new implementation, but will come ahead if object is created referenced to base class
But overriding actually overrides or provides new implementation permanently eventhough called referenced to base class
Hope this helps you. Plz mark it as answered if it helped you.