Difference Between Interface and Abstract Class in C#
Difference Between Interface and Abstract Class in C#
In object-oriented programming with C#, both interfaces
and abstract classes are used to achieve abstraction—a
fundamental principle that hides implementation details and exposes only
relevant functionality. However, they are used differently and serve different
purposes.
What is an
Interface?
- An
interface defines a contract: what a class must do, but not
how it does it.
- It
contains only method signatures, properties, events, or indexers.
- No implementation
is allowed (except default interface methods in C# 8.0+).
- A
class can implement multiple interfaces.
Example:
public interface
IAnimal
{
void Speak();
}
What is an
Abstract Class?
- An
abstract class is a partially implemented class.
- It
can contain both abstract members (without implementation) and concrete
members (with implementation).
- Cannot
be instantiated directly.
- A
class can inherit only one abstract class (single inheritance).
Example:
public abstract
class Animal
{
public abstract void Speak(); // no
implementation
public void Eat() // implemented method
{
Console.WriteLine("Eating...");
}
}
Key Differences
Between Interface and Abstract Class
Feature |
Interface |
Abstract Class |
Inheritance Type |
Multiple inheritance allowed |
Single inheritance only |
Method Implementation |
Cannot contain implementations (until C# 8+) |
Can have both abstract and concrete methods |
Constructor |
Cannot have constructors |
Can have constructors |
Fields/Variables |
Cannot contain fields |
Can contain fields and properties |
Access Modifiers |
All members are implicitly public |
Can have access modifiers (public,
protected, etc.) |
Instantiation |
Cannot be instantiated |
Cannot be instantiated |
Use Case |
Define a contract or capability |
Provide a common base with shared code |
When to Use
Use Interface when:
- You
want to define a common capability across unrelated classes.
- Multiple
inheritance is required.
- You’re
designing a plug-in architecture or dependency injection.
Use Abstract Class when:
- Classes
are closely related.
- You
need to share common code.
- You
want to define default behavior and also require derived classes to
implement specific features.
Example in Real
Use
public interface
IDriveable
{
void Drive();
}
public abstract
class Vehicle
{
public abstract void Start();
public void Fuel()
{
Console.WriteLine("Fueling
vehicle...");
}
}
public class Car
: Vehicle, IDriveable
{
public override void Start()
{
Console.WriteLine("Car
starting...");
}
public void Drive()
{
Console.WriteLine("Car
driving...");
}
}
Summary
- Interface:
Specifies what a class
should do, supports multiple inheritance, and promotes loose
coupling.
- Abstract class:
Provides a base class with some shared code, enforces a common base structure, and
supports code reuse.
Choosing between interface and abstract class
depends on your design goals—whether you need flexibility (interfaces) or
structure + shared logic (abstract classes).
Comments
Post a Comment