C# tutorials > Core C# Fundamentals > Object-Oriented Programming (OOP) > What is the difference between abstract classes and interfaces (including default interface methods)?
What is the difference between abstract classes and interfaces (including default interface methods)?
This tutorial explores the key differences between abstract classes and interfaces in C#, including the use of default interface methods. Understanding these differences is crucial for designing robust and maintainable object-oriented applications.
Core Differences: Abstract Classes vs. Interfaces
Both abstract classes and interfaces are fundamental tools for achieving abstraction in C#, but they serve different purposes and have distinct characteristics:
Abstract Class Example
This code defines an abstract class called Shape
. It includes an abstract method Area()
which must be implemented by any class that inherits from Shape
. It also includes a virtual method Description()
, which provides a default implementation that can be overridden by derived classes. Furthermore, it demonstrates the use of a property and a constructor, features not available in interfaces (before default interface methods).
public abstract class Shape
{
public abstract double Area();
public virtual string Description()
{
return "This is a shape.";
}
public string Color { get; set; }
public Shape(string color)
{
Color = color;
}
}
Interface Example
This code defines an interface called IPrintable
. It declares a single method, Print()
. Any class that implements IPrintable
must provide an implementation for the Print()
method.
public interface IPrintable
{
void Print();
}
Default Interface Methods (C# 8.0 and later)
C# 8.0 introduced default interface methods, allowing interfaces to provide a default implementation for methods. This enables interfaces to evolve without breaking existing implementations. In this example, the ILogger
interface defines a LogInformation
method with a default implementation. Classes implementing ILogger
can use the default implementation or provide their own override.
public interface ILogger
{
void Log(string message);
// Default implementation
void LogInformation(string message) => Log($"Information: {message}");
void LogWarning(string message);
void LogError(string message);
}
Implementing Abstract Class and Interface
This code demonstrates how a class, Circle
, can inherit from an abstract class, Shape
, and implement an interface, IPrintable
. It must provide an implementation for the abstract Area()
method from the Shape
class and the Print()
method from the IPrintable
interface. It also overrides the virtual Description()
method to provide a specific description for a circle. The constructor shows how the base class constructor is called using : base(color)
.
public class Circle : Shape, IPrintable
{
public double Radius { get; set; }
public Circle(double radius, string color) : base(color)
{
Radius = radius;
}
public override double Area()
{
return Math.PI * Radius * Radius;
}
public void Print()
{
Console.WriteLine($"Circle Area: {Area()}, Color: {Color}");
}
public override string Description()
{
return $"This is a circle with radius {Radius}.";
}
}
Concepts Behind the Snippets
Real-Life Use Case Section
Abstract Class: Imagine developing a game with different types of characters (e.g., enemy, player, NPC). You can define an abstract class Interface: Consider a scenario where you have different types of payment processors (e.g., credit card, PayPal, bank transfer). You can define an interface Character
with common properties like Health
, Position
, and abstract methods like Attack()
, Defend()
. Each character type would then inherit from Character
and implement the abstract methods in its own way.IPaymentProcessor
with methods like ProcessPayment()
, RefundPayment()
. Each payment processor class would implement IPaymentProcessor
, providing its specific implementation for processing payments.
Best Practices
Interview Tip
When asked about the difference between abstract classes and interfaces, highlight the core differences related to inheritance (single vs. multiple), implementation (partial vs. none), fields, and constructors. Also, mention the introduction of default interface methods in C# 8.0 and later, and how they allow interfaces to provide default implementations.
When to Use Them
Abstract Class: Interface:
Memory Footprint
The memory footprint difference between using abstract classes and interfaces is typically negligible. The primary factor influencing memory usage is the data stored within the objects created from classes that implement interfaces or inherit from abstract classes. However, the virtual method table (vtable) used for dynamic dispatch can introduce a minor overhead, but this is usually insignificant compared to the object's overall size.
Alternatives
Pros and Cons: Abstract Classes
Pros:
Cons:
Pros and Cons: Interfaces
Pros:
Cons:
FAQ
-
Can I inherit from multiple abstract classes?
No, C# only supports single inheritance for classes. You can only inherit from one abstract class. However, you can implement multiple interfaces. -
Can an interface have a constructor?
No, interfaces cannot have constructors. Constructors are used to initialize the state of an object, and interfaces define a contract without specifying an implementation. -
What are default interface methods and when should I use them?
Default interface methods (introduced in C# 8.0) allow you to add new methods to an interface without breaking existing implementations. Use them when you need to evolve an interface without forcing all implementing classes to immediately provide an implementation for the new method. Use them sparingly and consider the overall design carefully. -
When should I choose an abstract class over an interface?
Choose an abstract class when you have a clear 'is-a' relationship between classes and want to provide some common implementation and state (fields, properties). Also when you need to enforce a specific structure or behavior. Choose an interface when you want to define a contract that multiple, potentially unrelated classes can implement, focusing on behavior and decoupling.