Monday, 17 November 2025

Runtime Polymorphism

Runtime Polymorphism

Runtime Polymorphism in C#

Runtime polymorphism (dynamic polymorphism) means that the method to be executed is decided during runtime. It is achieved through method overriding using the virtual and override keywords.

Features:

  • Achieved using method overriding
  • Uses virtual and override
  • Method selection happens at runtime
  • Enables dynamic and flexible behavior

C# Example:

class Car { public virtual void Drive() { Console.WriteLine("Car is driving."); } } class SportsCar : Car { public override void Drive() { Console.WriteLine("SportsCar is driving fast!"); } } class Program { static void Main() { Car c = new SportsCar(); c.Drive(); } }

Output:

SportsCar is driving fast!

No comments:

Post a Comment