Polymorphism in C# refers to the ability of objects to take on different forms. It allows methods to behave differently based on the object that calls them, enabling flexibility and reusability in code.
Key Concepts of Polymorphism
Compile-Time Polymorphism (Static Polymorphism)
Achieved through method overloading and operator overloading.
The behavior is determined at compile time.
Run-Time Polymorphism (Dynamic Polymorphism)
Achieved through method overriding using inheritance.
The behavior is determined at runtime.
1. Compile-Time Polymorphism (Method Overloading)
Definition:
Method overloading allows multiple methods with the same name but different parameters in the same class.
Example:
classCalculator{publicintAdd(int a,int b){return a + b;}publicdoubleAdd(double a,double b){return a + b;}publicintAdd(int a,int b,int c){return a + b + c;}}// UsageCalculator calc =newCalculator();Console.WriteLine(calc.Add(5,3));// Output: 8Console.WriteLine(calc.Add(5.5,3.3));// Output: 8.8Console.WriteLine(calc.Add(1,2,3));// Output: 6
2. Run-Time Polymorphism (Method Overriding)
Definition:
A derived class overrides a base class method using the virtual and override keywords.
Example:
Polymorphism with Interfaces
Interfaces provide polymorphism by ensuring a class implements certain methods.
Example:
Key Terms
Term
Description
Method Overloading
Multiple methods with the same name but different signatures.
Method Overriding
Redefining a method in a derived class to change its behavior.
Virtual
Indicates that a method can be overridden in a derived class.
Override
Used in the derived class to override a virtual method.
Abstract
Defines a method without implementation in a base class, requiring derived classes to implement it.
Interface
Provides a contract for classes to implement methods, enabling polymorphism.
Differences Between Overloading and Overriding
Aspect
Overloading
Overriding
Definition
Same method name, different parameters.
Redefining a method in the derived class.
Polymorphism Type
Compile-time
Run-time
Keyword
No special keywords.
virtual, override, abstract
Class Relationship
Same class.
Requires inheritance.
Example Combining Overloading and Overriding
Example:
Summary
Polymorphism provides flexibility by allowing a single interface to represent different types of behavior.
Types:
Compile-Time: Method overloading and operator overloading.
Run-Time: Method overriding using inheritance.
Use polymorphism to enhance code reusability, maintainability, and extensibility.
Polymorphism is a cornerstone of object-oriented programming, ensuring better design and reduced redundancy.
class Animal {
public virtual void Speak() {
Console.WriteLine("The animal makes a sound.");
}
}
class Dog : Animal {
public override void Speak() {
Console.WriteLine("The dog barks.");
}
}
class Cat : Animal {
public override void Speak() {
Console.WriteLine("The cat meows.");
}
}
// Usage
Animal animal1 = new Dog();
animal1.Speak(); // Output: The dog barks.
Animal animal2 = new Cat();
animal2.Speak(); // Output: The cat meows.
interface IShape {
void Draw();
}
class Circle : IShape {
public void Draw() {
Console.WriteLine("Drawing a circle.");
}
}
class Rectangle : IShape {
public void Draw() {
Console.WriteLine("Drawing a rectangle.");
}
}
// Usage
IShape shape1 = new Circle();
shape1.Draw(); // Output: Drawing a circle.
IShape shape2 = new Rectangle();
shape2.Draw(); // Output: Drawing a rectangle.
class BaseClass {
public virtual void Display(string message) {
Console.WriteLine("Base class message: " + message);
}
}
class DerivedClass : BaseClass {
public override void Display(string message) {
Console.WriteLine("Derived class message: " + message);
}
// Overloading
public void Display(string message, int count) {
for (int i = 0; i < count; i++) {
Console.WriteLine(message);
}
}
}
// Usage
BaseClass baseObj = new BaseClass();
baseObj.Display("Hello from Base!"); // Output: Base class message: Hello from Base!
DerivedClass derivedObj = new DerivedClass();
derivedObj.Display("Hello from Derived!"); // Output: Derived class message: Hello from Derived!
derivedObj.Display("Hello again!", 3); // Output: Hello again! (3 times)