Lesson 3
Polymorphism in C#: Exploring Dynamic and Static Polymorphism
Introduction

Greetings! In today's lesson, we'll unravel the concept of polymorphism in C#'s Object-Oriented Programming (OOP). Grasping polymorphism enables us to use a single entity (a method, class, or interface) to represent different types in various scenarios. Let's proceed.

Seeing Polymorphism in Action

Polymorphism, a pillar of OOP, allows one object to embody multiple forms. Visualize a button in software; depending on its type (for instance, a submit button or a radio button), the action resulting from pressing it varies. This dynamic encapsulates the spirit of polymorphism!

C# supports various types of polymorphism. Run-time polymorphism, also known as dynamic polymorphism, occurs during runtime and leverages method overriding. Let's observe dynamic polymorphism in action within a simple application involving shapes. The base Shape class has an Area method, which calculates the area for shapes. This method is uniquely implemented in the subclasses Rectangle and Circle.

C#
1using System; 2 3abstract class Shape 4{ 5 public abstract double Area(); 6} 7 8class Rectangle : Shape 9{ 10 private double length; 11 private double width; 12 13 public Rectangle(double length, double width) 14 { 15 this.length = length; 16 this.width = width; 17 } 18 19 public override double Area() 20 { 21 return length * width; 22 } 23} 24 25class Circle : Shape 26{ 27 private double radius; 28 29 public Circle(double radius) 30 { 31 this.radius = radius; 32 } 33 34 public override double Area() 35 { 36 return Math.PI * radius * radius; 37 } 38} 39 40class Program 41{ 42 static void Main(string[] args) 43 { 44 Shape rectangle = new Rectangle(2, 3); 45 Console.WriteLine(rectangle.Area()); // Prints: 6.0 46 47 Shape circle = new Circle(5); 48 Console.WriteLine(circle.Area()); // Prints: 78.53981633974483 49 } 50}

Here, polymorphism shines as the Area() method takes on multiple forms while using the same Shape abstract class. It behaves differently depending on whether the object is a Rectangle or a Circle.

Method Overloading in C#: Static Polymorphism

The other type of polymorphism that C# supports is compile-time polymorphism, also known as static polymorphism, which operates during compile time and involves method overloading. Let's look at the example of static polymorphism:

C#
1using System; 2 3class MathOperations 4{ 5 // Overloaded method for rectangle area 6 public double Area(double length, double width) 7 { 8 return length * width; 9 } 10 11 // Overloaded method for circle area 12 public double Area(double radius) 13 { 14 return Math.PI * radius * radius; 15 } 16} 17 18class Program 19{ 20 static void Main(string[] args) 21 { 22 MathOperations mathOps = new MathOperations(); 23 Console.WriteLine(mathOps.Area(5, 2)); // Prints: 10 24 Console.WriteLine(mathOps.Area(3)); // Prints: 28.27 25 } 26}

In this example, polymorphism is demonstrated via method overloading. The Area method has multiple forms, accepting different parameter lists to compute the area of either a rectangle or a circle based on the provided arguments. If one argument is provided, the area method for the circle is called; if two arguments are provided, the area method for the rectangle is called.

This is a clear example of static polymorphism, where the correct method is determined at compile time.

Lesson Summary

Great job! We've now learned about polymorphism, observed its implementation, and discovered its applications. Now, prepare for hands-on practice tasks. Apply what you've learned and excel in C# programming. Happy coding!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.