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
What is the difference between function overriding and method hiding in C#?
In C#, function overriding and method hiding are two different mechanisms for changing method behavior in derived classes. While both allow a child class to provide its own implementation of a parent class method, they work in fundamentally different ways.
Function Overriding
Function overriding allows a subclass to provide a specific implementation of a method that is already defined in its parent class. The parent method must be marked as virtual, abstract, or override, and the child method uses the override keyword.
Syntax
// Parent class
public virtual ReturnType MethodName() { }
// Child class
public override ReturnType MethodName() { }
Example
using System;
abstract class Shape {
public abstract int Area();
}
class Rectangle : Shape {
private int length;
private int width;
public Rectangle(int a = 0, int b = 0) {
length = a;
width = b;
}
public override int Area() {
Console.WriteLine("Rectangle class area:");
return (width * length);
}
}
class RectangleTester {
static void Main(string[] args) {
Rectangle r = new Rectangle(10, 7);
double a = r.Area();
Console.WriteLine("Area: {0}", a);
}
}
The output of the above code is −
Rectangle class area: Area: 70
Method Hiding (Shadowing)
Method hiding, also known as shadowing, allows a child class to define a new method with the same name as a parent class method. The child class uses the new keyword to hide the parent method rather than override it.
Syntax
// Parent class
public ReturnType MethodName() { }
// Child class
public new ReturnType MethodName() { }
Example
using System;
class Demo {
public class Parent {
public string Display() {
return "Parent Class!";
}
}
public class Child : Parent {
public new string Display() {
return "Child Class!";
}
}
static void Main(string[] args) {
Child child = new Child();
Console.WriteLine(child.Display());
Parent parent = new Child();
Console.WriteLine(parent.Display());
}
}
The output of the above code is −
Child Class! Parent Class!
Key Differences
| Aspect | Function Overriding | Method Hiding |
|---|---|---|
| Keyword Used | override |
new |
| Parent Method Requirement | Must be virtual, abstract, or override
|
Can be any method |
| Polymorphic Behavior | Yes − calls child implementation via parent reference | No − calls parent implementation via parent reference |
| Runtime Binding | Late binding (dynamic) | Early binding (static) |
Demonstrating the Difference
using System;
class Vehicle {
public virtual void Start() {
Console.WriteLine("Vehicle starting...");
}
public void Stop() {
Console.WriteLine("Vehicle stopping...");
}
}
class Car : Vehicle {
public override void Start() {
Console.WriteLine("Car engine starting...");
}
public new void Stop() {
Console.WriteLine("Car brakes applied...");
}
}
class Program {
static void Main(string[] args) {
Vehicle v = new Car();
Console.WriteLine("=== Overriding (virtual/override) ===");
v.Start(); // Calls Car's Start() method
Console.WriteLine("=== Method Hiding (new) ===");
v.Stop(); // Calls Vehicle's Stop() method
Console.WriteLine("=== Direct Car Reference ===");
Car c = new Car();
c.Stop(); // Calls Car's Stop() method
}
}
The output of the above code is −
=== Overriding (virtual/override) === Car engine starting... === Method Hiding (new) === Vehicle stopping... === Direct Car Reference === Car brakes applied...
Conclusion
Function overriding provides true polymorphism with late binding, where the child method is called even through a parent reference. Method hiding creates a separate method that shadows the parent method but doesn't provide polymorphic behavior − the parent method is called when accessed via a parent reference.
