Complete C# Tutorial

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 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 of Circle and Square to the IShape 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 a ProcessPayment() method.
  • CreditCard, PayPal, and Bitcoin implement IPayment in different ways.
  • In Main(), we assign different payment methods to the IPayment type and call ProcessPayment().
  • 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. ๐Ÿš€

Leave a Comment

Share this Doc

Polymorphism with Interfaces

Or copy link