Complete C# Tutorial

Abstract Classes vs Interfaces in C# - Key Differences & Examples

πŸš€ Introduction

Hey there, C# learner! πŸ‘‹ Have you ever been confused about when to use an abstract class and when to use an interface? You’re not alone! This is a common question for many beginners.

Both abstract classes and interfaces help define a blueprint for other classes, but they work in different ways. In this lesson, we will break it all down in a super simple way. So, grab your coffee β˜• and let’s get started!

🧐 What is an Abstract Class in C#?

An abstract class is a class that cannot be instantiated (you cannot create an object of it). It can have both abstract methods (without a body) and concrete methods (with a body).

Β 

✨ Syntax of an Abstract Class

				
					abstract class Animal  // Abstract class
{
    public abstract void MakeSound();  // Abstract method (no body)
    
    public void Sleep()  // Concrete method (has a body)
    {
        Console.WriteLine("Sleeping... 😴");
    }
}
				
			

🧐 What is an Interface in C#?

An interface is like a contract that defines only method signatures but not their implementation. Any class that implements an interface must provide the method definitions.

Β 

✨ Syntax of an Interface

				
					interface IAnimal  // Interface
{
    void MakeSound();  // Method with no body (must be implemented)
}
				
			

πŸ†š Key Differences: Abstract Classes vs Interfaces in C#

FeatureAbstract ClassInterface
πŸ› Instantiation❌ Cannot be instantiated❌ Cannot be instantiated
πŸ“ Method Implementationβœ… Can have both abstract & concrete methods❌ Only method declarations, no implementation (until C# 8.0)
⚑ Fields & Propertiesβœ… Can have fields, properties, and constructors❌ Cannot have fields, only properties (from C# 8.0)
🏷 Multiple Inheritance❌ A class can inherit only one abstract classβœ… A class can implement multiple interfaces
πŸ”„ Use CaseUsed when a base class provides some default behaviorUsed when different classes should follow the same contract

πŸ‘¨β€πŸ’» Example 1: Using Abstract Class

Let’s say we are creating a program for different types of animals.

Β 

✨ Code Example

				
					using System;

abstract class Animal  // Abstract class
{
    public abstract void MakeSound();  // Abstract method
    
    public void Sleep()  // Concrete method
    {
        Console.WriteLine("Sleeping... 😴");
    }
}

class Dog : Animal  // Inheriting abstract class
{
    public override void MakeSound()  // Implementing abstract method
    {
        Console.WriteLine("Dog says: Woof Woof 🐢");
    }
}

class Program
{
    static void Main()
    {
        Dog myDog = new Dog();
        myDog.MakeSound();
        myDog.Sleep();
    }
}
				
			

πŸ–₯ Output

				
					Dog says: Woof Woof 🐢
Sleeping... 😴
				
			

βœ… Explanation:

  • The Animal class has an abstract method MakeSound() and a concrete method Sleep().
  • The Dog class inherits from Animal and provides an implementation for MakeSound().

πŸ‘¨β€πŸ’» Example 2: Using Interface

Now, let’s implement the same example using an interface.

Β 

✨ Code Example

				
					using System;

interface IAnimal  // Interface
{
    void MakeSound();  // No body
}

class Cat : IAnimal  // Implementing interface
{
    public void MakeSound()  // Must provide implementation
    {
        Console.WriteLine("Cat says: Meow Meow 🐱");
    }
}

class Program
{
    static void Main()
    {
        Cat myCat = new Cat();
        myCat.MakeSound();
    }
}
				
			

πŸ–₯ Output

				
					Cat says: Meow Meow 🐱
				
			

βœ… Explanation:

  • The IAnimal interface only defines the MakeSound() method.
  • The Cat class implements the interface and provides the method definition.

🌍 Real-World Example

Imagine you are designing a payment system where different payment methods (Credit Card, PayPal, UPI) must implement a MakePayment() method.

Β 

Using an Interface:

				
					interface IPayment
{
    void MakePayment(double amount);
}

class CreditCard : IPayment
{
    public void MakePayment(double amount)
    {
        Console.WriteLine($"Paid {amount} using Credit Card πŸ’³");
    }
}

class PayPal : IPayment
{
    public void MakePayment(double amount)
    {
        Console.WriteLine($"Paid {amount} using PayPal 🏦");
    }
}

class Program
{
    static void Main()
    {
        IPayment payment1 = new CreditCard();
        payment1.MakePayment(100);

        IPayment payment2 = new PayPal();
        payment2.MakePayment(200);
    }
}
				
			

πŸ–₯ Output

				
					Paid 100 using Credit Card πŸ’³
Paid 200 using PayPal 🏦
				
			

βœ… Explanation:

  • Different payment methods follow the same contract (MakePayment() method).
  • This ensures loose coupling and makes the code flexible to add more payment methods in the future.

πŸ† When to Use What?

βœ”οΈ Use Abstract Classes When:

  • You need to share some common behavior between subclasses.
  • You want to provide method implementation along with abstract methods.

βœ”οΈ Use Interfaces When:

  • You need to define only method signatures.
  • You want to allow multiple inheritance in your code.

Β 

🎯 Conclusion

Phew! That was a lot of learning! πŸ˜ƒ Now you know how Abstract Classes and Interfaces work in C#.

  • Abstract classes help define a base class with common behavior.
  • Interfaces define a contract that multiple classes must follow.
  • Both are useful, and knowing when to use which makes you a better C# developer.

So, next time you’re writing code, think:

πŸ€” “Do I need a common base class?” β†’ Use Abstract Class
πŸ€” “Do I need a contract for multiple classes?” β†’ Use Interface

Β 

πŸ”₯ Next What?

In the next chapter, you will learn Abstract Classes with Constructors in C#. So, stay tuned! πŸš€

Leave a Comment

Share this Doc

Abstract Classes vs Interfaces

Or copy link