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 You Are Going to Learn
βοΈ What is an abstract class and how to use it?
βοΈ What is an interface and when to use it?
βοΈ The key differences between abstract classes and interfaces
βοΈ Real-world examples to understand their usage
βοΈ Practical C# programs with complete code and output
π§ 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#
Feature | Abstract Class | Interface |
---|---|---|
π 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 Case | Used when a base class provides some default behavior | Used 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 methodMakeSound()
and a concrete methodSleep()
. - The
Dog
class inherits fromAnimal
and provides an implementation forMakeSound()
.
π¨βπ» 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 theMakeSound()
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! π