Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Difference between Method Overriding and Method Hiding in C#
In C#, there are two mechanisms for redefining or providing new implementation of a method from a parent class in its child class: Method Overriding and Method Hiding. Understanding the difference between these concepts is crucial for implementing proper inheritance and polymorphism.
Method overriding uses the override keyword and enables runtime polymorphism, while method hiding uses the new keyword and provides compile-time method resolution.
Syntax
Following is the syntax for method overriding −
public class BaseClass {
public virtual void Method() { }
}
public class DerivedClass : BaseClass {
public override void Method() { }
}
Following is the syntax for method hiding −
public class BaseClass {
public void Method() { }
}
public class DerivedClass : BaseClass {
public new void Method() { }
}
Example of Method Overriding
Method overriding provides runtime polymorphism where the derived class method is called even when accessed through a base class reference −
using System;
public class Animal {
public virtual void MakeSound() {
Console.WriteLine("Animal makes a sound");
}
}
public class Dog : Animal {
public override void MakeSound() {
Console.WriteLine("Dog barks");
}
}
public class Program {
public static void Main() {
Animal animal = new Dog();
animal.MakeSound(); // Calls Dog's method
Dog dog = new Dog();
dog.MakeSound(); // Calls Dog's method
}
}
The output of the above code is −
Dog barks Dog barks
Example of Method Hiding
Method hiding provides compile-time method resolution where the method called depends on the reference type −
using System;
public class Vehicle {
public void Start() {
Console.WriteLine("Vehicle starts");
}
}
public class Car : Vehicle {
public new void Start() {
Console.WriteLine("Car starts with key");
}
}
public class Program {
public static void Main() {
Vehicle vehicle = new Car();
vehicle.Start(); // Calls Vehicle's method
Car car = new Car();
car.Start(); // Calls Car's method
}
}
The output of the above code is −
Vehicle starts Car starts with key
Comparison
| Aspect | Method Overriding | Method Hiding |
|---|---|---|
| Keyword Used | override |
new |
| Base Class Requirement | Method must be virtual, abstract, or override
|
Any method (no special requirement) |
| Polymorphism Type | Runtime polymorphism | Compile-time method resolution |
| Method Called via Base Reference | Derived class method | Base class method |
| Access to Base Implementation | Available using base.MethodName()
|
Hidden, not accessible through derived class |
| Purpose | Provide different implementation | Completely replace base functionality |
Accessing Base Implementation
In method overriding, you can access the base class implementation using the base keyword −
using System;
public class Shape {
public virtual void Draw() {
Console.WriteLine("Drawing a shape");
}
}
public class Circle : Shape {
public override void Draw() {
base.Draw(); // Calls base implementation
Console.WriteLine("Drawing a circle");
}
}
public class Program {
public static void Main() {
Shape shape = new Circle();
shape.Draw();
}
}
The output of the above code is −
Drawing a shape Drawing a circle
Conclusion
Method overriding uses override keyword and provides runtime polymorphism, while method hiding uses new keyword and provides compile-time method resolution. Choose overriding for polymorphic behavior and hiding when you need to completely replace base functionality without polymorphism.
