What is abstract class vs interface in c

Object-oriented programming (OOP) has transformed the way developers approach software design by organizing code around objects, rather than functions or logic. Central to OOP are concepts like encapsulation, inheritance, and polymorphism, which enable programmers to create modular, flexible, and reusable code.

Encapsulation allows for the bundling of data and functions that operate on that data within a single unit (an object). Inheritance facilitates the creation of new classes based on existing ones, while polymorphism allows objects of different types to be treated as if they are objects of a common supertype. These principles foster clean, scalable, and maintainable code.

Although C is not traditionally considered an OOP language like C++ or C#, it is still possible to implement OOP-like structures using advanced techniques. Developers often use abstract classes and interfaces to achieve modularity and flexibility in their design, helping to simulate some of the benefits of object-oriented design

Understanding these concepts, especially abstract classes and interfaces is essential for programmers looking to create flexible, reusable, and scalable code in C, particularly those transitioning to fully object-oriented languages such as C++ or C#.

Enroll in our Software Engineering Bootcamp to learn more about how to design flexible, reusable, scalable code.

For C specific courses, head over to our Learn C Programming from Scratch and Intermediate C++ 20 Programming courses.

In this blog, we’ll break down the concepts of abstract classes and interfaces in C, and how they can be applied effectively to improve the design of your programs.

Key Differences Between Abstract Classes and Interfaces in C

Understanding the differences between abstract classes and interfaces is essential for designing flexible, modular, and maintainable code, especially when transitioning from procedural C programming language to object-oriented languages. These concepts are core to object-oriented programming (OOP) and are often used to model real-world behaviors and system architectures.

Let’s explore these differences in more depth:

Implementation vs. Blueprint

An abstract class serves as a partial implementation of an object. It can contain both implemented and non-implemented (abstract) methods. This means an abstract class not only defines a blueprint for derived classes but also provides some shared functionality that child classes can inherit and use. This makes abstract classes a hybrid between a concrete class and a fully abstract interface.

For instance, if you have several types of animals (e.g., Dog, Cat), you might define an abstract class Animal with shared methods such as eat() or sleep(). Each specific animal class would then override methods like makeSound(), which is defined as abstract in the Animal class.

By contrast, an interface defines a purely abstract contract. It does not provide any implemented functionality; rather, it specifies a set of methods that must be implemented by any class that adheres to the interface. In C, an interface is simulated with a structure that contains function pointers, but no actual implementation. This forces all implementing classes to provide their own versions of the specified methods.

Inheritance

In languages like C++, a class can inherit from only one abstract class, meaning you can only extend from one base class. This restriction encourages a clear hierarchy where a derived class extends from a parent and inherits its behavior. However, in C, abstract classes are simulated using structures, so direct inheritance is not a language feature. Instead, composition is used, where a structure contains another structure, and function pointers are used to override behaviors.

One of the key strengths of interfaces is their flexibility—classes can implement multiple interfaces. This allows a class to conform to multiple "contracts," enabling objects to perform different roles or behaviors that may not be related. In C, you can simulate this by including multiple structures (each representing an interface) within a single structure. This promotes a more flexible design that allows mixing and matching different behaviors.

Member Variables and Constructors

Abstract classes can have member variables, which can store state, and constructors, which allow you to initialize this state when the object is created. This is particularly useful for scenarios where you need to share common attributes or initialization logic across derived classes. For example, if every animal has a name, an abstract Animal class could store this data, and the derived Dog and Cat classes could inherit this property.

Interfaces cannot contain member variables or constructors. Their purpose is purely to define behavior without specifying any implementation details or maintaining state. This keeps interfaces lightweight and focused solely on defining methods. This difference in C is expressed through the absence of data fields in the interface structure—only function pointers are included.

Usage in Code Design

Use abstract classes when you have a base class that provides both common behavior and abstract methods. Abstract classes work well when several related classes share some code but also need to implement their own versions of certain methods. For example, if you’re creating different types of shapes (e.g., Circle, Rectangle), an abstract Shape class could provide methods for calculating area or perimeter, but the specific formulas would vary based on the shape type.

Abstract classes are also useful when you need to maintain a shared state, such as an identifier or name, and when that state needs to be passed down to all subclasses.

Interfaces are better suited for defining multiple behaviors that unrelated classes can implement. For example, a class might need to be both "swimmable" and "flyable." These behaviors don’t necessarily have to be related, but both can be defined via interfaces, allowing unrelated classes to implement the required methods. In this case, you would use separate interfaces for swimming and flying.

This flexibility makes interfaces ideal for situations where different classes must share common methods but don’t share any implementation. For example, a Vehicle interface could define methods like drive() and stop(), but a Car and a Boat could implement them in entirely different ways.

Characteristics of Abstract Classes in C

In object-oriented programming (OOP), an abstract class serves as a blueprint for other classes and cannot be instantiated directly. It often contains methods with and without implementation, providing a mix of abstract and concrete behavior. While C is not an OOP language, you can simulate abstract classes by combining structures with function pointers, allowing for polymorphism and modular code design.

Here are a few key characteristics shared by C abstract classes:

Contains Both Defined and Abstract Methods

Abstract classes can have both methods with implementation and abstract (pure virtual) methods. The abstract methods act as placeholders for derived classes to implement, while the methods with implementation provide shared functionality that can be reused by derived classes. This allows for partial implementation of behavior in the abstract class while leaving some flexibility for subclasses to provide their own specific implementations.


For example, an abstract class in C might have a method like makeSound(), which is left unimplemented in the abstract class and must be provided by any derived class.

typedef struct {
    void (*makeSound)(void);  // Abstract method
} Animal;

Derived Classes Must Implement All Abstract Methods

Any class that inherits from an abstract class must implement all of its abstract methods. In C, this means that structures which simulate inheritance must provide concrete implementations for any function pointers declared in the abstract class. This requirement ensures that the derived classes provide behavior for the abstract methods, preserving the integrity of the program design.


For example, if Animal has an abstract method makeSound(), any derived class such as Dog must provide a concrete implementation for this method.

// Derived class: Dog
void dogMakeSound(void) {
    printf("Woof!\n");
}

Abstract Classes Can Have Member Variables

Abstract classes in C can contain member variables. These variables allow the class to maintain a state that can be shared across derived classes. In C, this can be achieved by defining variables within the structure. Derived classes can then inherit these variables, providing them with a common data structure while allowing them to implement different behaviors.

For example, an abstract Animal class might have a member variable such as name to store the name of the animal, and derived classes like Dog or Cat can access or modify this member.

Abstract Classes Can Have Constructors

Although C doesn't support constructors in the same way that C++ or C# does, you can simulate constructors in C by creating functions that initialize structures. This allows you to set default values for member variables or function pointers. 

For example, an "abstract class" in C can have an initialization function that sets up the structure and assigns default behaviors, which can then be modified by derived classes.

Characteristics of Interfaces in C

In object-oriented programming (OOP), interfaces play a crucial role in defining behavior without enforcing how that behavior should be implemented. While C is not inherently object-oriented, you can simulate interfaces using structures with function pointers, allowing different structures to follow the same contract. In C, an interface defines a set of methods without implementation, and any structure that "implements" the interface must provide concrete implementations for all these methods.

Here are some key characteristics of interfaces in C:

No Implementation in Interfaces

An interface does not provide any method implementations. It is a purely abstract structure that defines a set of function pointers. Unlike abstract classes, which may contain some methods with implementation, an interface is solely a contract that defines what methods an implementing class (or structure) must include. This enforces a clear separation between what needs to be done (the method signatures) and how it should be done (the concrete implementations).

In C, an interface is represented as a structure with function pointers, but without any actual code to implement the methods:

typedef struct {
    void (*start)();  // Abstract method
    void (*stop)();   // Abstract method
} Vehicle;

No Member Variables or Constructors

Unlike abstract classes, interfaces cannot contain member variables or constructors. Their sole purpose is to define method signatures. This keeps interfaces lightweight and focused on behavior rather than state. In C, this is reflected by the absence of any data fields in the interface structure. All structure contains are function pointers.

This means you cannot store attributes or maintain any internal state in an interface. The responsibility of maintaining state (such as variables like speed or fuelLevel) lies with the structures that implement the interface.

typedef struct {
    void (*start)();  // No member variables, only function pointers
    void (*stop)();
} Vehicle;

Concrete Implementations Required for All Methods

Any structure that implements an interface must provide concrete implementations for all the methods defined by the interface. This ensures that any structure claiming to implement an interface adheres to the full contract of the interface, providing consistent behavior across different implementations.

For example, if the Vehicle interface defines start() and stop() methods, then a Car or Motorcycle structure implementing the Vehicle interface must implement both methods:

// Implementing the Interface
void carStart() {
    printf("Car is starting\n");
}
void carStop() {
    printf("Car is stopping\n");
}
Vehicle myCar = { .start = carStart, .stop = carStop };

The myCar instance now implements the Vehicle interface, adhering to its contract by providing definitions for both start() and stop() methods.

Supports Multiple Inheritance

One of the key advantages of interfaces is that they allow a form of multiple inheritance, which abstract classes cannot. In C, a structure can implement multiple interfaces by including function pointers from multiple interface structures. This allows a single structure to conform to multiple behaviors without being restricted to a single inheritance hierarchy.

For instance, consider two interfaces: Vehicle (for starting and stopping) and Machine (for maintenance). A Car structure can implement both:

typedef struct {
    void (*start)();
    void (*stop)();
} Vehicle;

typedef struct {
    void (*performMaintenance)();
} Machine;

// Implementing both interfaces
void carStart() {
    printf("Car is starting\n");
}

void carStop() {
    printf("Car is stopping\n");
}

void carMaintenance() {
    printf("Car is undergoing maintenance\n");
}

// Car structure implements both Vehicle and Machine interfaces
typedef struct {
    Vehicle vehicle;
    Machine machine;
} Car;

Car myCar = { .vehicle = { .start = carStart, .stop = carStop }, 
              .machine = { .performMaintenance = carMaintenance } };

In this example, Car implements both Vehicle and Machine interfaces, allowing it to adhere to both sets of behaviors. This promotes greater flexibility in design by enabling multiple behavior definitions that can be implemented by different structures.

Interfaces are Ideal for Defining Roles or Behaviors

Interfaces are best suited for defining roles or behaviors that different objects can implement. For example, in a program where different objects (e.g., Car, Boat, Plane) have the ability to start and stop, the Vehicle interface can define those behaviors. Each object (structure) that implements the Vehicle interface must provide specific implementations of those behaviors, such as how a Car starts versus how a Boat starts.

The ability to define different behaviors for unrelated classes makes interfaces a powerful tool for designing flexible, reusable code. This is particularly useful when multiple objects share the same behavior but implement it in different ways.

Leverage the power of interfaces in C to decouple your code. Check out our Learn C Programming from Scratch and Intermediate C++ 20 Programming in-depth courses to get started!

When to Use Abstract Classes in C

Abstract classes are a powerful tool in object-oriented programming, even when simulated in C. They allow for code reuse, modularity, and scalability by defining common behaviors and properties that multiple derived classes can inherit and modify as needed. 

Here are key scenarios in which you should consider using abstract classes in C:

  • When multiple derived classes share common functionality: Abstract classes allow you to define shared behavior that can be inherited by multiple derived classes, reducing code duplication.
  • When you anticipate future extensions: Abstract classes provide a flexible foundation that can easily be extended with new features or functionality without major code changes.
  • When different implementations share common properties or methods: Abstract classes let you define common properties and methods, while allowing derived classes to implement their own specific behavior.
  • When you want to provide default implementations for some methods: Abstract classes enable you to offer default behavior for some methods while requiring derived classes to override others.
  • When you need a strong class hierarchy: Abstract classes enforce a well-organized class structure, ensuring consistency and clear inheritance patterns throughout your codebase.

Abstract classes in C are useful when designing code that requires shared functionality across multiple derived classes. They provide flexibility, reusability, and the ability to extend your code in the future.

When to Use Interfaces in C

Interfaces are a powerful tool in designing flexible, modular, and loosely coupled systems, especially when simulating object-oriented programming concepts in C. While abstract classes provide a way to share common behavior across related classes, interfaces allow you to enforce specific behaviors across different, potentially unrelated, classes.

Here are key scenarios in which interfaces should be used in C:

  • When you want to implement multiple functionalities across unrelated classes: Interfaces allow a structure to implement several unrelated behaviors, providing more flexibility in design.
  • When you want to promote loose coupling and code maintainability: Interfaces enforce a contract without dictating the implementation, reducing dependencies between components and improving maintainability.
  • When you need to define a standard contract for different implementations: Interfaces ensure that different classes adhere to the same set of method signatures, providing consistency across varying implementations.
  • When you want to enable flexibility through multiple implementations: Interfaces allow different classes to implement the same behavior in their own unique way, promoting modularity and reuse.
  • When you want to decouple code from specific implementations: Interfaces abstract the implementation details, allowing you to switch out underlying logic without affecting higher-level code.

Interfaces in C offer a way to enforce behavior while promoting flexibility, loose coupling, and maintainability. They allow you to define a set of methods that must be implemented by any structure, regardless of the specifics of how the methods are implemented.

Best Practices for Using Abstract Classes and Interfaces

Understanding when and how to use abstract classes and interfaces is crucial for building scalable, maintainable, and modular code.

Abstract Classes Should Adhere to the Single Responsibility Principle (SRP)

The Single Responsibility Principle (SRP) suggests that a class should only have one reason to change, meaning it should focus on providing a single set of closely related functionalities. When using abstract classes, it’s important to avoid cramming too many unrelated responsibilities into one class. Doing so not only violates SRP but also makes the code harder to maintain, extend, and debug.

For instance, if you have an abstract class Vehicle, it should only contain methods and properties related to basic vehicle behaviors like start(), stop(), or speed. Any specialized responsibilities like flying or sailing should be defined in separate classes or interfaces. This keeps the class hierarchy clean and ensures that the abstract class provides a clear, focused functionality.

Use Abstract Classes for Shared Code, Interfaces for Multiple Inheritance or Pure Behavioral Contracts

One of the key considerations when choosing between abstract classes and interfaces is whether you need to share code or define a strict behavioral contract:

  • Abstract Classes: Use abstract classes when you have some shared behavior that multiple derived classes can inherit and reuse. Abstract classes are ideal when there’s a need for common functionality across different classes, but you still want to allow flexibility in certain methods (via abstract methods).
  • Interfaces: Use interfaces when you want to enforce a specific behavior without prescribing how that behavior should be implemented. Interfaces are particularly useful when you need multiple inheritance or when classes need to adhere to a contract but may be unrelated.

Keep Class Structure Simple and Intuitive

A common pitfall when working with abstract classes and interfaces is over-complicating the design. It’s tempting to create multiple abstract methods or use too many interfaces, but this can lead to complex, difficult-to-read code that becomes hard to maintain. To avoid this, strive to keep your class structure simple and intuitive by limiting the number of abstract methods or interfaces to what is necessary.

Leverage Abstract Classes for Code Reuse

One of the primary reasons to use abstract classes is to facilitate code reuse. When several classes share common functionality, abstract classes allow you to centralize that shared code while still leaving room for flexibility through abstract methods. This reduces code duplication and makes it easier to maintain.

 For example, if multiple types of vehicles (e.g., Car, Bike, Truck) need to implement start() and stop() methods in the same way, placing these methods in an abstract Vehicle class makes sense. Derived classes can then reuse the common code while overriding only the methods that differ, such as accelerate() or refuel().

Choosing Between Abstract Classes and Interfaces in C

Both abstract classes and interfaces serve specific purposes in object-oriented design. Abstract classes allow you to define shared functionality and state, while interfaces provide a way to enforce a contract of behaviors across unrelated classes. 

Abstract classes are ideal when you have common behavior or code that can be inherited and reused by multiple derived classes, whereas interfaces are best suited when you need multiple inheritance or want to enforce a set of behaviors without dictating how those behaviors are implemented.

If your classes share common functionality, an abstract class can streamline your code by reducing duplication and promoting reuse. However, if you need to define multiple behaviors that can be implemented by different and unrelated classes, interfaces will provide the flexibility you need while promoting loose coupling in your system design.

Ready to deepen your understanding of C programming? Explore our C programming certification courses such as Learn C Programming from Scratch and Intermediate C++ 20 Programming to master advanced techniques like abstract classes and interfaces.