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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Pradeep ShetRank #595 · 4K reputationPosted Feb 12, 2015, 3:11 AM EST
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.
Nirav VasoyaRank #8,316 · 224 reputationPosted Apr 21, 2015, 1:09 AM EDT
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.
Rajeev KumarRank #1,334 · 1.3K reputationPosted Mar 16, 2023, 6:36 AM EDT
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".
Tuhin PaulRank #7 · 71.9K reputationPosted Mar 16, 2023, 10:30 AM EDT
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.
Tuhin PaulRank #7 · 71.9K reputationPosted Mar 16, 2023, 10:30 AM EDT
Example of Method Hiding
public class SuperClass { public static void printMessage() { System.out.println("Hello from SuperClass"); } } public class SubClass extends SuperClass { public static void printMessage() { System.out.println("Hello from SubClass"); } } public static void main(String[] args) { SuperClass.printMessage(); // prints "Hello from SuperClass" SubClass.printMessage(); // prints "Hello from SubClass" SuperClass baseRef = new SubClass(); baseRef.printMessage(); // prints "Hello from SuperClass" }Tuhin PaulRank #7 · 71.9K reputationPosted Mar 16, 2023, 10:31 AM EDT
Example of Method Overriding:
public class SuperClass { public void printMessage() { System.out.println("Hello from SuperClass"); } } public class SubClass extends SuperClass { @Override public void printMessage() { System.out.println("Hello from SubClass"); } } public static void main(String[] args) { SuperClass baseRef = new SubClass(); baseRef.printMessage(); // prints "Hello from SubClass" }Tuhin PaulRank #7 · 71.9K reputationPosted Mar 21, 2023, 12:04 PM EDT
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.