1. Method Overloading:
Definition: Method overloading occurs when two or more methods in the same class have the same name but differ in their parameters (either in number, type, or both). It is a way to create multiple methods with the same name but different functionality based on the parameters passed.
- Overloading is determined at compile time (also called static polymorphism).
- The return type of overloaded methods can be the same or different, but the method signature (name + parameters) must be different.
Example of Method Overloading:
csharp
using System;
class Calculator
{
// Method with one parameter
public int Add(int a)
{
return a + 10;
}
// Overloaded method with two parameters
public int Add(int a, int b)
{
return a + b;
}
// Overloaded method with three parameters
public int Add(int a, int b, int c)
{
return a + b + c;
}
}
class Program
{
static void Main()
{
Calculator calc = new Calculator();
Console.WriteLine(calc.Add(5)); // Output: 15
Console.WriteLine(calc.Add(5, 10)); // Output: 15
Console.WriteLine(calc.Add(5, 10, 20)); // Output: 35
}
}
In the above example, the method Add
is overloaded three times. The method name is the same, but the parameter count differs each time.
2. Method Overriding:
Definition: Method overriding occurs when a method in a derived class has the same name, return type, and parameters as a method in the base class. The method in the derived class overrides the method in the base class to provide specific implementation.
- Overriding is determined at runtime (also called dynamic polymorphism).
- The method in the base class must be marked as
virtual
(orabstract
), and the method in the derived class must be marked with theoverride
keyword.
Example of Method Overriding:
csharp
using System;
class Animal
{
// Virtual method in the base class
public virtual void Speak()
{
Console.WriteLine("Animal speaks");
}
}
class Dog : Animal
{
// Overriding the Speak method in the derived class
public override void Speak()
{
Console.WriteLine("Dog barks");
}
}
class Program
{
static void Main()
{
Animal myAnimal = new Animal();
myAnimal.Speak(); // Output: Animal speaks
Dog myDog = new Dog();
myDog.Speak(); // Output: Dog barks
Animal animalRef = new Dog();
animalRef.Speak(); // Output: Dog barks (runtime polymorphism)
}
}
In the example above:
- The method
Speak
is defined asvirtual
in theAnimal
class, which allows it to be overridden in theDog
class. - The
Speak
method in theDog
class provides a new implementation, and this is known as method overriding. - Notice the behavior when we use an
Animal
reference (animalRef
) to refer to aDog
object. The method is resolved at runtime (dynamic polymorphism), and the overridden method in theDog
class is called.
Key Differences:
Feature | Method Overloading | Method Overriding |
---|---|---|
Definition | Same method name, different parameters | Same method name and signature in base/derived classes |
Binding Time | Compile-time (Static Polymorphism) | Runtime (Dynamic Polymorphism) |
Inheritance | No inheritance required | Must involve inheritance (base and derived classes) |
Keyword Used | No special keyword required | override in the derived class, virtual in the base class |
Return Type | Can be different, but doesn't affect overloading | Must have the same return type in both base and derived classes |
Summary:
- Method Overloading is when multiple methods with the same name but different parameters exist in the same class.
- Method Overriding is when a method in a derived class provides its own implementation for a method already defined in the base class.