Interfaces vs Abstract Classes in C# Polymorphism
Quick Answer
In C#, interfaces define contracts with no implementation, allowing multiple inheritance, while abstract classes provide a base with shared code and state. Use interfaces for loosely coupled designs and abstract classes when sharing common behavior. Both enable polymorphism but serve different design needs.
Learning Objectives
- Explain the purpose of Interfaces vs Abstract Classes in a practical learning context.
- Identify the main ideas, terms, and decisions involved in Interfaces vs Abstract Classes.
- Apply Interfaces vs Abstract Classes in a simple real-world scenario or practice task.
Introduction
Polymorphism is a core concept in object-oriented programming that allows objects to be treated as instances of their parent class or interface.
In C#, interfaces and abstract classes are two key tools to implement polymorphism, but they serve different purposes and have distinct features.
Understanding when to use interfaces versus abstract classes is essential for designing flexible and maintainable software.
“Favor composition over inheritance, and prefer interfaces over abstract classes when possible.”
What is an Interface in C#?
An interface in C# defines a contract that classes or structs can implement. It contains declarations of methods, properties, events, or indexers without any implementation.
Interfaces enable multiple inheritance of type, allowing a class to implement multiple interfaces.
They are ideal for defining capabilities or behaviors that can be shared across unrelated classes.
- Contains only method/property/event signatures without implementation.
- Supports multiple inheritance.
- Cannot contain fields or constructors.
- Used to define capabilities or roles.
What is an Abstract Class in C#?
An abstract class is a class that cannot be instantiated on its own and may contain both abstract members (without implementation) and concrete members (with implementation).
It is used to provide a common base with shared code and state for derived classes.
Abstract classes support single inheritance, meaning a class can inherit from only one abstract class.
- Can contain fields, constructors, and implemented methods.
- Supports single inheritance.
- Can define both abstract and concrete members.
- Used to share common behavior among related classes.
Key Differences Between Interfaces and Abstract Classes
Choosing between interfaces and abstract classes depends on design requirements such as inheritance needs, shared implementation, and flexibility.
| Feature | Interface | Abstract Class |
|---|---|---|
| Implementation | No implementation (until default interface methods in C# 8+) | Can have full or partial implementation |
| Inheritance | Supports multiple inheritance | Supports single inheritance |
| Fields | Cannot have fields | Can have fields |
| Constructors | Cannot have constructors | Can have constructors |
| Use Case | Define capabilities or contracts | Share common base behavior and state |
| Versioning |
When to Use Interfaces vs Abstract Classes
Use interfaces when you want to define a contract that multiple unrelated classes can implement, especially when multiple inheritance is needed.
Use abstract classes when you want to share common code or state among closely related classes and enforce a base class.
- Use interfaces for loose coupling and flexibility.
- Use abstract classes to avoid code duplication.
- Interfaces are better for defining capabilities like IDisposable or IEnumerable.
- Abstract classes are better for base classes with default behavior.
Example: Interface vs Abstract Class in C#
Let's look at a practical example demonstrating both an interface and an abstract class to implement polymorphism.
Interface Example
Here, the interface defines a contract for a payment method.
Abstract Class Example
The abstract class provides a base implementation for different types of employees.
Practical Example
This interface defines a Pay method that any payment method class must implement.
The abstract class Employee provides a base with a shared property and method, while requiring derived classes to implement CalculateSalary.
Examples
public interface IPaymentMethod
{
void Pay(decimal amount);
}
public class CreditCardPayment : IPaymentMethod
{
public void Pay(decimal amount)
{
Console.WriteLine($"Paid {amount:C} using Credit Card.");
}
}This interface defines a Pay method that any payment method class must implement.
public abstract class Employee
{
public string Name { get; set; }
public Employee(string name)
{
Name = name;
}
public abstract decimal CalculateSalary();
public void DisplayEmployee()
{
Console.WriteLine($"Employee: {Name}");
}
}
public class Manager : Employee
{
public Manager(string name) : base(name) { }
public override decimal CalculateSalary()
{
return 8000m;
}
}The abstract class Employee provides a base with a shared property and method, while requiring derived classes to implement CalculateSalary.
Best Practices
- Use interfaces to define capabilities that can be shared across unrelated classes.
- Use abstract classes to share common code and state among related classes.
- Prefer interfaces when you need multiple inheritance of type.
- Avoid adding implementation to interfaces unless using default interface methods in C# 8 or later.
- Keep interfaces focused and minimal to maintain flexibility.
- Use abstract classes when you want to provide default behavior and enforce a common base.
Common Mistakes
- Using abstract classes when multiple inheritance is needed.
- Adding too many members to interfaces, making them hard to implement.
- Trying to instantiate abstract classes directly.
- Confusing interfaces with classes and expecting them to hold state.
- Ignoring versioning issues when modifying interfaces.
Hands-on Exercise
Implement a Shape Interface and Abstract Class
Create an interface IShape with a method CalculateArea. Then create an abstract class Shape that implements IShape and adds a property for color. Implement two concrete classes Circle and Rectangle.
Expected output: Classes Circle and Rectangle that calculate area and have a color property.
Hint: Use interface for the method signature and abstract class for shared properties.
Interview Questions
What is the main difference between an interface and an abstract class in C#?
InterviewAn interface defines a contract with no implementation and supports multiple inheritance, while an abstract class can provide shared implementation and state but supports only single inheritance.
When should you use an interface instead of an abstract class?
InterviewUse an interface when you want to define a capability that multiple unrelated classes can implement, especially if multiple inheritance is required.
Can an abstract class implement an interface in C#?
InterviewYes, an abstract class can implement interfaces and provide either abstract or concrete implementations of the interface members.
MCQ Quiz
1. What is the best first step when learning Interfaces vs Abstract Classes?
A. Understand the purpose and basic idea
B. Skip directly to advanced implementation
C. Ignore examples and practice
D. Memorize terms without context
Correct answer: A
Starting with the purpose and basic idea makes later examples and practice easier to understand.
2. Which activity helps reinforce Interfaces vs Abstract Classes?
A. Reading once without practice
B. Building or writing a small practical example
C. Avoiding review questions
D. Skipping the summary
Correct answer: B
A small practical example helps connect the topic to real usage.
3. Which statement is most accurate about this topic?
A. In C#, interfaces define contracts with no implementation, allowing multiple inheritance, while abstract classes provide a base with shared code and state.
B. Interfaces vs Abstract Classes never needs examples
C. Interfaces vs Abstract Classes is unrelated to practical work
D. Interfaces vs Abstract Classes should be learned without checking results
Correct answer: A
The correct option is based on the available topic explanation.
Key Takeaways
- In C#, interfaces define contracts with no implementation, allowing multiple inheritance, while abstract classes provide a base with shared code and state.
- Use interfaces for loosely coupled designs and abstract classes when sharing common behavior.
- Both enable polymorphism but serve different design needs.
- Polymorphism is a core concept in object-oriented programming that allows objects to be treated as instances of their parent class or interface.
- In C#, interfaces and abstract classes are two key tools to implement polymorphism, but they serve different purposes and have distinct features.
Summary
Interfaces and abstract classes are fundamental tools for implementing polymorphism in C#.
Interfaces define contracts without implementation and support multiple inheritance, making them ideal for defining capabilities.
Abstract classes allow sharing code and state among related classes, supporting single inheritance.
Choosing between them depends on design goals such as flexibility, code reuse, and inheritance needs.
Frequently Asked Questions
Can a class implement multiple interfaces in C#?
Yes, a class can implement multiple interfaces, enabling multiple inheritance of type.
Can an interface contain method implementations?
Starting with C# 8.0, interfaces can have default implementations, but traditionally interfaces only declare method signatures.
Can abstract classes have constructors?
Yes, abstract classes can have constructors which are called by derived classes.
Is it possible to instantiate an abstract class?
No, abstract classes cannot be instantiated directly.
What is Interfaces vs Abstract Classes?
In C#, interfaces define contracts with no implementation, allowing multiple inheritance, while abstract classes provide a base with shared code and state.
Why is Interfaces vs Abstract Classes important?
Use interfaces for loosely coupled designs and abstract classes when sharing common behavior.

