Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 5

Abstract Classes vs. Interfaces: Differences and Use Cases

Recommended resources

In our last session, we explored how inheritance and method overriding allow us to build hierarchies of related classes, enabling polymorphism and code reuse. We saw how a derived class can specialize the behavior of its parent. Today, we'll take this a step further by examining two powerful C# constructs for defining contracts and blueprints: abstract classes and interfaces.

This topic is a cornerstone of object-oriented design and a frequent subject in technical interviews. Your goal is not just to define these concepts, but to clearly articulate the crucial differences between them and, most importantly, explain the design trade-offs involved in choosing one over the other. Given your background in software engineering, you'll recognize these as fundamental tools for creating loosely coupled, maintainable, and extensible systems.

1. Abstract Classes: A Blueprint with Implementation

An abstract class serves as a common base for a group of closely related classes. Think of it as a partially implemented blueprint. It defines the common structure and behavior that all its descendants will share, but it cannot be instantiated on its own. It establishes an "is-a" relationship, just like standard inheritance.

An abstract class is declared using the abstract keyword. Its defining feature is that it can contain a mix of different member types:

  • Abstract methods: These are declared with the abstract keyword and have no implementation ({}). Any non-abstract class that inherits from the abstract class must provide an implementation for all its abstract methods.
  • Concrete methods: These are regular methods with a full implementation. This shared code is inherited by all derived classes as-is.
  • Virtual methods: As we saw in the previous lesson, these have a default implementation but can be overridden by derived classes.
  • Fields and Properties: Abstract classes can hold state (data), which is shared among all derived classes.
  • Constructors: They can have constructors, which are called during the instantiation of a derived class.

The following resource provides a clear definition and code example.

Abstract Class vs Interface in C# - NDepend Blog

This section from the NDepend blog provides a concise definition of abstract classes and a practical example using a ShapeBase class.

Read the section What is an Abstract Class?, paying close attention to the ShapeBase code example. Note how it mixes an abstract method (Draw), a concrete method (Translate), and a virtual method (CalculateArea). Then, examine how the Square class must override the abstract Draw method.

The primary use case for an abstract class is to provide a common foundation and share implementation details within a tight-knit family of classes. For example, a DatabaseConnection abstract class could contain common retry logic in a concrete method, while leaving the Connect() and ExecuteQuery() methods as abstract for specific implementations like SqlConnection and OracleConnection to define.


2. Interfaces: A Pure Contract

An interface is a completely abstract type that defines a contract of capabilities. It specifies what a class should do, but provides no implementation for how it should do it. An interface establishes a "can-do" or "has-a-behavior" relationship. By convention, interface names in C# are prefixed with a capital 'I', such as IDisposable or ISerializable.

Traditionally, interfaces could only contain declarations for:

  • Methods
  • Properties
  • Events
  • Indexers

They could not contain implementation, fields, or constructors. A class that implements an interface must provide an implementation for all of its members. The most significant feature of interfaces is that a single class can implement multiple interfaces.

Abstract Class vs Interface in C# - NDepend Blog

This section of the NDepend blog defines interfaces and shows a simple IShape example.

Read the section What is an Interface? and look at the IShape code. Notice that it only contains method signatures; there is no implementation. The section also lists the pros of using interfaces, such as enabling multiple implementations and loose coupling, which are critical for good architectural design.

Interfaces are ideal for defining capabilities that can be applied to unrelated classes. For example, a Car, a Document, and a NetworkStream are all very different things, but they might all implement the ISerializable interface, indicating they have the capability to be converted into a byte stream.


3. Abstract Class vs. Interface: The Core Differences

Being able to clearly articulate the differences is a key interview skill. While modern C# versions have slightly blurred the lines (e.g., C# 8.0 introduced default interface methods), the fundamental design distinctions remain.

This short video frames the topic perfectly for an interview discussion.

Interfaces vs Abstract Classes

This video from Raw Coding provides a high-level overview of the conceptual differences before diving into the syntax.

First, watch the brief conceptual overview from the general idea to understand the core purpose: abstract classes for commonality, interfaces for decoupling. Next, focus on the syntactical differences. This part covers access modifiers, fields, constructors, and how methods are consumed. Finally, the segment on how you would use them explains the crucial distinction between inheritance ("is a") and implementation ("has this behavior").

Let's consolidate these differences. The image below shows the basic relationship: a class extends an abstract class but implements an interface.

This diagram visually contrasts the relationships. The `Circle` and `Rectangle` classes 'extend' a single `Shape` abstract class, inheriting from it. In contrast, `Circle` and `Rectangle` 'implement' the `Shape` interface, fulfilling its contract.

Here is a summary of the most important distinctions for your interview:

Feature Abstract Class Interface
Inheritance A class can inherit from one abstract class. A class can implement multiple interfaces.
Purpose Defines the identity of an object and shares common code. Establishes an "is-a" relationship. Defines a capability or contract. Establishes a "can-do" relationship.
Implementation Can contain a mix of implemented (concrete) and unimplemented (abstract) methods. Traditionally contains no implementation, only member signatures. (Default implementations are possible in C# 8.0+ but serve a different purpose).
State Can contain fields and constructors to manage state. Cannot contain instance fields or constructors; it is stateless.
Keywords Uses abstract and override. Class extends another class. Uses interface. Class implements an interface.

It's crucial to use the correct terminology: a class inherits from or extends an abstract class, but it implements an interface. Using this language correctly signals a deeper understanding.


4. When to Use Which? A Design Decision

Choosing between an abstract class and an interface is a fundamental design decision. Here are the guiding principles.

  • Use an abstract class when:

    • You want to share code and state among several closely related classes.
    • You expect that classes that extend your abstract class have many common methods or fields, or require access modifiers other than public.
    • You want to define a common base for a component family that can be versioned over time. You can add new concrete methods to the base class without breaking the derived classes.
  • Use an interface when:

    • You expect that unrelated classes will implement the interface. For example, IComparable and ICloneable are implemented by many different types.
    • You want to define the behavior of a type, but not its implementation.
    • You need to take advantage of multiple inheritance of type.
    • You want to facilitate decoupled architectures and enable techniques like dependency injection and mocking for unit tests.

In modern software development, there is a strong preference for "composition over inheritance." This principle favors building complex functionality by composing smaller, single-responsibility objects rather than inheriting from large, monolithic base classes. Interfaces are the key enabler of this pattern.

The following video discusses this trade-off with a fantastic practical example.

Interfaces vs Abstract Classes

The second half of the Raw Coding video provides an excellent discussion of the inheritance vs. composition trade-off.

Watch the section from the big debate. The presenter implements a notification system first using an abstract class (inheritance) and then with interfaces (composition). Pay attention to the conclusion: inheritance results in succinct code but couples the logic together, while composition is more flexible and reusable but can be more verbose. This is exactly the kind of nuanced analysis an interviewer looks for.


5. The "Pure" Abstract Class Dilemma

A common point of confusion arises: "If I create an abstract class where all members are abstract, isn't it the same as an interface?"

The answer is no, and this reveals a critical design flaw.

Abstract Class vs Interface in C# | C# Interview Questions | Csharp Interview Questions

This video from Questpond explains exactly why a "pure" abstract class is a bad idea by demonstrating the multiple inheritance problem it creates.

Watch the segment from if you stretch this abstract class. The presenter shows how making an abstract Customer class fully abstract makes it look like an interface. However, when a new requirement arises (a Lead who is like a customer but has no discount), the design breaks. Attempting to fix it with two abstract classes fails because a concrete class (WalkInCustomer) cannot inherit from both. This powerfully demonstrates why interfaces, with their support for multiple implementation, are essential for this kind of flexible "plug-and-play" design.

The key takeaway is that even if an abstract class technically can be made to mimic an interface, you are trapping yourself. You lose the primary advantage of interfaces—the ability for a class to adopt multiple contracts—which is fundamental to building flexible and scalable systems.


Conclusion

You've now covered the definitions, differences, and design considerations for abstract classes and interfaces in C#. These concepts are not just about syntax; they are about architectural intent.

Here are the key takeaways for your interview:

  • Abstract classes are for sharing code and a common identity within a family of related classes ("is-a"). They are partially implemented blueprints.
  • Interfaces are for defining a capability or a contract that can be adopted by any class, regardless of where it is in the class hierarchy ("can-do"). They are pure contracts.
  • The most critical difference is single inheritance (abstract classes) vs. multiple implementations (interfaces).
  • In modern design, there is a preference for interfaces and composition to build flexible, loosely coupled systems, although abstract classes remain the right tool for sharing common base functionality.

In our next lesson, we will dive into the SOLID principles of object-oriented design. You will see how the concepts of abstract classes and interfaces are absolutely central to principles like the Interface Segregation Principle and the Dependency Inversion Principle, which are the hallmarks of a professional software engineer.