Chapter 13: Polymorphism

1. What is Polymorphism? (Super Simple Analogy)

Imagine you have a remote control with a big “Play” button:

  • When you press “Play” on a Music Player โ†’ it plays songs ๐ŸŽต
  • When you press “Play” on a DVD Player โ†’ it plays movies ๐ŸŽฌ
  • When you press “Play” on a Game Console โ†’ it starts the game ๐ŸŽฎ

The same button (“Play”) does different things depending on what device itโ€™s connected to. Thatโ€™s polymorphism in real life!

In C#:

  • You define a common interface or base class with a method (like Play())
  • Different derived classes provide their own version of that method
  • You can call the method the same way on all objects, but each one behaves differently

2. Method Overriding โ€“ The Foundation of Polymorphism

We already saw this in the Inheritance chapter, but now weโ€™ll use it for true polymorphic behavior.

Key points:

  • Base class marks method as virtual
  • Derived class provides new implementation with override
  • You can call the method through a base class reference โ†’ it runs the derived class version

Example โ€“ Animals Making Sounds

C#

Polymorphic usage โ€“ treating all as Animal

C#

Output:

text

Magic moment: Even though the array is of type Animal[], each call to MakeSound() runs the correct derived class version โ€” thatโ€™s polymorphism!

3. Abstract Classes & Abstract Methods โ€“ Forcing Implementation

Sometimes you want a base class to be a template that cannot be used directly, and you force every child to provide its own implementation.

Abstract class โ†’ cannot be instantiated (new Animal() would fail) Abstract method โ†’ has no body in base class โ€“ must be overridden in every derived class

C#

Polymorphic usage:

C#

Output:

text

Key rule: Every non-abstract derived class must implement all abstract methods from the base class.

4. Polymorphic Behavior in Real Projects

You can store different types in the same collection and treat them uniformly.

Example โ€“ Payment Processor

C#

Mini-Project: Vehicle Rental System with Polymorphism

C#

Summary โ€“ What We Learned Today

  • Polymorphism = “many forms” โ€“ same method call, different behavior
  • virtual + override โ†’ classic overriding
  • Abstract classes & methods โ†’ force derived classes to implement
  • Polymorphic collections โ†’ store different types in base type array/list
  • Real power โ†’ write code that works with any derived class without knowing the exact type

Your Homework (Super Practical!)

  1. Create a new console project called PolymorphismMaster
  2. Create an abstract class Employee with:
    • Properties: Name, BaseSalary
    • Abstract method: CalculateBonus()
    • Concrete method: GetFullInfo()
  3. Create at least 3 derived classes:
    • Developer โ†’ bonus = 10% of salary + โ‚น500 per project
    • Manager โ†’ bonus = 15% + โ‚น2000 per team member
    • Intern โ†’ bonus = fixed โ‚น2000
  4. In Program.cs: Create a list of employees (mixed types), loop through them, and print name + total salary (base + bonus)

Next lesson: Interfaces โ€“ weโ€™re going to learn how to make classes even more flexible without inheritance!

Youโ€™re doing absolutely fantastic! ๐ŸŽ‰ Any part confusing? Want more examples with abstract classes or polymorphism? Just tell me โ€” Iโ€™m right here for you! ๐Ÿ’™

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *