Complete C# Tutorial

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 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 from Animal.
  • 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 method MakeSound().
  • The Dog and Cat 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 method CalculateSalary().
  • FullTimeEmployee and PartTimeEmployee 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! πŸš€πŸ’»

Leave a Comment

Share this Doc

Polymorphism with Abstract Classes

Or copy link