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 run time polymorphism in C#?
Runtime polymorphism in C# allows the same method call to behave differently depending on the actual type of object at runtime. This is achieved through method overriding, also known as dynamic binding or late binding. It is implemented using abstract classes and virtual functions.
In runtime polymorphism, the method to be called is determined at runtime based on the object's actual type, not the reference type declared at compile time.
Using Abstract Classes
Abstract classes contain abstract methods that must be implemented by derived classes. This ensures that each derived class provides its own specific implementation −
Example
using System;
abstract class Shape {
public abstract double Area();
public void Display() {
Console.WriteLine("Shape area: " + Area());
}
}
class Rectangle : Shape {
private double length;
private double width;
public Rectangle(double l, double w) {
length = l;
width = w;
}
public override double Area() {
return length * width;
}
}
class Circle : Shape {
private double radius;
public Circle(double r) {
radius = r;
}
public override double Area() {
return Math.PI * radius * radius;
}
}
class Program {
static void Main(string[] args) {
Shape s1 = new Rectangle(10, 7);
Shape s2 = new Circle(5);
s1.Display();
s2.Display();
}
}
The output of the above code is −
Shape area: 70 Shape area: 78.5398163397448
Using Virtual Functions
Virtual functions allow a base class to define a default implementation that can be overridden in derived classes. The virtual keyword in the base class and override keyword in derived classes enable runtime polymorphism −
Example
using System;
class Animal {
public virtual void MakeSound() {
Console.WriteLine("Animal makes a sound");
}
}
class Dog : Animal {
public override void MakeSound() {
Console.WriteLine("Dog barks: Woof!");
}
}
class Cat : Animal {
public override void MakeSound() {
Console.WriteLine("Cat meows: Meow!");
}
}
class Program {
static void Main(string[] args) {
Animal[] animals = { new Dog(), new Cat(), new Animal() };
foreach (Animal animal in animals) {
animal.MakeSound(); // Runtime polymorphism in action
}
}
}
The output of the above code is −
Dog barks: Woof! Cat meows: Meow! Animal makes a sound
Runtime vs Compile-time Polymorphism
| Runtime Polymorphism | Compile-time Polymorphism |
|---|---|
Method overriding using virtual and override
|
Method overloading and operator overloading |
| Decision made at runtime | Decision made at compile time |
| Uses inheritance and virtual functions | Uses multiple methods with same name but different signatures |
| Also called dynamic or late binding | Also called static or early binding |
Conclusion
Runtime polymorphism in C# enables dynamic method binding, where the actual method called is determined at runtime based on the object's type. This powerful feature is achieved through method overriding using abstract classes or virtual functions, allowing for flexible and extensible code design.
