Polymorphism with Abstract Classes in C# (Easy Guide with Examples)
π― Introduction
Hey there! Ever felt confused about abstract classes and polymorphism? Donβt worry! Today, we are going to break it down in the simplest way possible.
Polymorphism with abstract classes in C# allows us to define methods in a base class and override them in derived classes. This is super useful when you want a common structure but also need customization.
Let’s explore it with examples! π
π What You Will Learn in This Lesson
βοΈ What is polymorphism with abstract classes in C#?
βοΈ Why do we need it?
βοΈ Syntax and explanation
βοΈ Simple and real-world examples
βοΈ Code with output and detailed explanation
π What is Polymorphism with Abstract Classes in C#?
Polymorphism means “many forms.” When combined with abstract classes, it allows us to create a blueprint in the parent class and modify it in child classes.
β Why is it Important?
- Ensures code reusability.
- Provides flexibility while maintaining structure.
- Helps in achieving clean and maintainable code.
Let’s first understand abstract classes before we dive into polymorphism!
π Syntax of Abstract Classes in C#
abstract class Animal
{
public abstract void MakeSound(); // Abstract method
}
class Dog : Animal
{
public override void MakeSound()
{
Console.WriteLine("Dog says: Woof Woof!");
}
}
π Explanation:
abstract class Animal
: This is the base class.public abstract void MakeSound();
: This method must be overridden in derived classes.class Dog : Animal
: The child class inherits fromAnimal
.public override void MakeSound()
: This method implements the abstract method.
Now, letβs see a complete working program!
π» Example 1: Polymorphism with Abstract Classes in C#
using System;
abstract class Animal
{
public abstract void MakeSound();
}
class Dog : Animal
{
public override void MakeSound()
{
Console.WriteLine("Dog says: Woof Woof!");
}
}
class Cat : Animal
{
public override void MakeSound()
{
Console.WriteLine("Cat says: Meow Meow!");
}
}
class Program
{
static void Main()
{
Animal myDog = new Dog();
myDog.MakeSound();
Animal myCat = new Cat();
myCat.MakeSound();
}
}
β Output
Dog says: Woof Woof!
Cat says: Meow Meow!
π Explanation:
- We create an abstract class
Animal
with an abstract methodMakeSound()
. - The
Dog
andCat
classes override this method with their own implementation. - In
Main()
, we create objects of derived classes using the parent class reference (Animal myDog = new Dog();
). - This allows polymorphism, meaning the same method
MakeSound()
behaves differently based on the object type.
π’ Real-World Example: Employee System
Imagine you are building a salary calculator for different types of employees. Some employees get a fixed salary, while others are paid hourly.
Let’s see how polymorphism with abstract classes in C# helps!
using System;
abstract class Employee
{
public string Name;
public Employee(string name)
{
Name = name;
}
public abstract void CalculateSalary();
}
class FullTimeEmployee : Employee
{
public FullTimeEmployee(string name) : base(name) {}
public override void CalculateSalary()
{
Console.WriteLine($"{Name}'s salary is $5000 per month.");
}
}
class PartTimeEmployee : Employee
{
public PartTimeEmployee(string name) : base(name) {}
public override void CalculateSalary()
{
Console.WriteLine($"{Name}'s salary is $20 per hour.");
}
}
class Program
{
static void Main()
{
Employee emp1 = new FullTimeEmployee("Alice");
emp1.CalculateSalary();
Employee emp2 = new PartTimeEmployee("Bob");
emp2.CalculateSalary();
}
}
β Output
Alice's salary is $5000 per month.
Bob's salary is $20 per hour.
π Explanation:
- The
Employee
class is abstract and has an abstract methodCalculateSalary()
. FullTimeEmployee
andPartTimeEmployee
override this method with their own salary structures.- In
Main()
, we use polymorphism (Employee emp1 = new FullTimeEmployee("Alice");
), allowing different behaviors for the same method call.
Isnβt this cool? You can extend this to many real-world cases like banking systems, billing software, and role-based access control! π
π Key Takeaways
βοΈ Abstract classes provide a blueprint.
βοΈ Method overriding enables polymorphism.
βοΈ Useful for real-world scenarios like salary systems, animal sounds, etc.
βοΈ Makes code structured, reusable, and scalable.
Β
π― Conclusion
Congratulations! π You just learned Polymorphism with Abstract Classes in C# with real-world examples.
Now, you understand how abstract classes and method overriding work together
to create flexible and scalable code.
Β
π Next What?
In the next chapter, you will learn OOPs – Abstract Classes in C#! Get ready for another exciting lesson! π―
Keep practicing and exploring! If you have any doubts, feel free to ask. Happy coding! ππ»