Module 3 - Chapter 3: Polymorphism in C#

Polymorphism, a core concept in object-oriented programming (OOP), enables objects of different classes to be treated as objects of a common superclass. It's a Greek word that means "having multiple forms," which is exactly what polymorphism allows in programming: the same method or property can be used in different ways for different objects. C# implements polymorphism primarily through method overriding and interfaces, allowing for dynamic method resolution at runtime. This chapter explores the concept of polymorphism in C# and how it can be used to design flexible and easily extendable software.

Mar 28, 2024

Polymorphism in C#

Polymorphism, a core concept in object-oriented programming (OOP), enables objects of different classes to be treated as objects of a common superclass. It's a Greek word that means "having multiple forms," which is exactly what polymorphism allows in programming: the same method or property can be used in different ways for different objects. C# implements polymorphism primarily through method overriding and interfaces, allowing for dynamic method resolution at runtime. This chapter explores the concept of polymorphism in C# and how it can be used to design flexible and easily extendable software.

Understanding Polymorphism

Polymorphism can be achieved in two ways in C#: Compile-time polymorphism and Runtime polymorphism.

Compile-Time Polymorphism

Also known as method overloading, compile-time polymorphism allows multiple methods in the same class to have the same name but different parameters.
public class Calculator { public int Add(int a, int b) { return a + b; } public double Add(double a, double b) { return a + b; } }

Runtime Polymorphism

Runtime polymorphism is achieved through method overriding, where a method in a base class is redefined in the derived class. The method that gets called is determined at runtime.
public class Animal { public virtual void Speak() { Console.WriteLine("The animal speaks"); } } public class Dog : Animal { public override void Speak() { Console.WriteLine("Woof"); } }

Using virtual and override Keywords

The virtual keyword is used in the base class to indicate that a method can be overridden in any derived class. The override keyword is used in the derived class to redefine the base class method.

Interface Polymorphism

Polymorphism can also be implemented using interfaces. An interface defines a contract (set of methods and properties) that all classes implementing the interface must follow. This allows for objects of different classes that implement the same interface to be treated interchangeably.
public interface IShape { void Draw(); } public class Circle : IShape { public void Draw() { Console.WriteLine("Drawing a circle"); } } public class Square : IShape { public void Draw() { Console.WriteLine("Drawing a square"); } }
In this example, both Circle and Square implement the IShape interface. This means that both classes can be referred to by their interface type, allowing for polymorphic behavior.

Benefits of Polymorphism

Polymorphism offers several benefits in software development:
  • Flexibility: It allows for the implementation of flexible software architectures that can be extended and modified with minimal changes to the existing code.
  • Reusability: Polymorphic code can work with objects of different classes, allowing for the reuse of code.
  • Maintainability: By decoupling the code, polymorphism helps in creating systems that are easier to maintain and update.

Summary

Polymorphism is a powerful concept in OOP that enhances the flexibility and maintainability of code. In C#, polymorphism allows you to use objects of different classes interchangeably, provided they share the same superclass or interface. By mastering polymorphism, you'll be able to write more concise, modular, and dynamic C# programs that can easily adapt to changing requirements.