Mastering Polymorphism with Interfaces in C#
๐ Introduction
Hey there, future C# pro! ๐ฏ Have you ever wondered how different classes can share a common behavior while implementing their own unique way of doing things? Thatโs where Polymorphism with Interfaces in C# comes in!
Imagine youโre playing a video game. You press the same “Jump” button, but different characters (a human, a robot, or a bird) jump in their own unique ways. Thatโs polymorphism in action! ๐ฎ
๐ What You Are Going to Learn in This Lesson
โ๏ธ What is Polymorphism with Interfaces in C#?
โ๏ธ Why do we need it?
โ๏ธ Syntax and explanation ๐
โ๏ธ Hands-on coding examples ๐ป
โ๏ธ A real-world example to make it crystal clear ๐
โ๏ธ Common mistakes and best practices ๐
๐ง What is Polymorphism with Interfaces in C#?
Polymorphism means “many forms.” When a class implements an interface, it provides its own version of the methods declared in that interface.
An interface only defines what needs to be done, but the class decides how to do it!
ย
โ Why is Polymorphism with Interfaces in C# Important?
โ
Encourages code reusability
โ
Supports multiple inheritance (since a class can implement multiple interfaces)
โ
Makes code flexible and scalable
โ
Helps decouple code for better maintainability
ย
๐ Syntax of Interfaces in C#
interface IAnimal
{
void MakeSound(); // Method without implementation
}
class Dog : IAnimal
{
public void MakeSound()
{
Console.WriteLine("Dog says: Woof! ๐ถ");
}
}
class Cat : IAnimal
{
public void MakeSound()
{
Console.WriteLine("Cat says: Meow! ๐ฑ");
}
}
๐ป Example 1: Basic Polymorphism with Interfaces
using System;
interface IShape
{
void Draw();
}
class Circle : IShape
{
public void Draw()
{
Console.WriteLine("Drawing a Circle ๐ข");
}
}
class Square : IShape
{
public void Draw()
{
Console.WriteLine("Drawing a Square ๐ฅ");
}
}
class Program
{
static void Main()
{
IShape shape1 = new Circle();
IShape shape2 = new Square();
shape1.Draw();
shape2.Draw();
}
}
๐ Output:
Drawing a Circle ๐ข
Drawing a Square ๐ฅ
๐ Explanation
- IShape is an interface that defines a method
Draw()
. - Circle and Square classes implement the interface and provide their own versions of
Draw()
. - In
Main()
, we use polymorphism by assigning objects ofCircle
andSquare
to theIShape
type. - When we call
Draw()
, it executes the method specific to the actual object.
ย
๐ Real-World Example: Payment System
Imagine an online shopping website ๐. Users can pay with a credit card, PayPal, or cryptocurrency. Each payment method follows the same process but has different implementations.
using System;
interface IPayment
{
void ProcessPayment(double amount);
}
class CreditCard : IPayment
{
public void ProcessPayment(double amount)
{
Console.WriteLine($"Processing Credit Card Payment of ${amount} ๐ณ");
}
}
class PayPal : IPayment
{
public void ProcessPayment(double amount)
{
Console.WriteLine($"Processing PayPal Payment of ${amount} ๐
ฟ๏ธ");
}
}
class Bitcoin : IPayment
{
public void ProcessPayment(double amount)
{
Console.WriteLine($"Processing Bitcoin Payment of ${amount} โฟ");
}
}
class Program
{
static void Main()
{
IPayment paymentMethod;
paymentMethod = new CreditCard();
paymentMethod.ProcessPayment(100);
paymentMethod = new PayPal();
paymentMethod.ProcessPayment(50);
paymentMethod = new Bitcoin();
paymentMethod.ProcessPayment(200);
}
}
๐ Output:
Processing Credit Card Payment of $100 ๐ณ
Processing PayPal Payment of $50 ๐
ฟ๏ธ
Processing Bitcoin Payment of $200 โฟ
๐ Explanation
IPayment
defines aProcessPayment()
method.CreditCard
,PayPal
, andBitcoin
implementIPayment
in different ways.- In
Main()
, we assign different payment methods to theIPayment
type and callProcessPayment()
. - The correct method runs based on the actual object.
ย
โ ๏ธ Common Mistakes and Best Practices
โ Mistake: Forgetting to implement all methods from an interface.
โ
Fix: Always provide implementations for all interface methods.
โ Mistake: Trying to create an object of an interface.
โ
Fix: You cannot instantiate an interface directly. Use a class that implements it.
ย
๐ฏ Conclusion
You did it! ๐ Now you understand Polymorphism with Interfaces in C# and why itโs so powerful. Interfaces allow us to enforce a contract in different classes while enabling flexibility. Whether itโs drawing shapes, processing payments, or handling game characters, interfaces make it easy to achieve polymorphism.
๐ Keep practicing! Try creating your own interfaces and implementing them in different ways.
ย
โญ๏ธ Next What?
In the next chapter, you will learn Polymorphism with Abstract Classes in C#. It’s going to be another fun ride! Stay tuned. ๐