Tutorials Home   >   Object-Oriented Programming (OOP)   >   Abstract Class vs Interface

Abstract Class vs Interface

In Object-Oriented Programming (OOP), programmers often want to define a set of methods that other classes must implement. Two important tools for this are abstract classes and interfaces.

Both abstract classes and interfaces allow abstraction, but they have different rules and purposes. Understanding the difference is essential to write clean, flexible, and reusable code.

In this explanation, you will learn:

  • What an abstract class is

  • What an interface is

  • The differences between them

  • Examples in programming

  • Advantages and common mistakes


1. What Is an Abstract Class?

An abstract class is a class that cannot be instantiated directly. It may contain:

  • Abstract methods – declared but not implemented

  • Concrete methods – fully implemented methods

  • Attributes – variables that can store data

Simple Definition:

An abstract class is a partially completed class that provides a blueprint for other classes.


Example in Python

from abc import ABC, abstractmethod

# Abstract class
class Vehicle(ABC):
@abstractmethod
def move(self):
pass # Abstract method, no implementation

def fuel_type(self):
print(“Diesel or Petrol”) # Concrete method

# Child class
class Car(Vehicle):
def move(self):
print(“Car moves on roads”)

# Object creation
myCar = Car()
myCar.move() # Output: Car moves on roads
myCar.fuel_type() # Output: Diesel or Petrol

Here:

  • Vehicle is abstract and cannot be instantiated

  • move() is abstract and must be implemented by child classes

  • fuel_type() is concrete and inherited as-is


2. What Is an Interface?

An interface is a fully abstract type that defines only method signatures, without any implementation.

  • All methods in an interface are abstract

  • Classes that implement an interface must provide implementation for all its methods

  • Interfaces cannot have attributes or concrete methods (in most languages)

Simple Definition:

An interface is a contract that a class must follow.


Example in Java

// Interface
interface Vehicle {
void move(); // Abstract method
}
// Implementing class
class Car implements Vehicle {
public void move() {
System.out.println(“Car moves on roads”);
}
}

public class Main {
public static void main(String[] args) {
Car myCar = new Car();
myCar.move(); // Output: Car moves on roads
}
}

Here:

  • Vehicle is an interface

  • Car implements the interface

  • move() is required in the implementing class


3. Key Differences Between Abstract Class and Interface

Feature Abstract Class Interface
Instantiation Cannot be instantiated Cannot be instantiated
Methods Can have abstract and concrete methods Only abstract methods (mostly)
Attributes Can have member variables Usually cannot have attributes (constants only)
Inheritance Supports single or multilevel inheritance Can be implemented by multiple classes (multiple inheritance)
Purpose Share code among related classes Define a contract for unrelated classes
Access Modifiers Methods can have public, protected Methods are usually public (depending on language)
Constructor Can have a constructor Cannot have a constructor
Example Vehicle abstract class with fuel_type() Vehicle interface with only move()

4. When to Use an Abstract Class

  1. Classes are closely related (share common code)

  2. Some methods need default implementation

  3. You want to store attributes in the parent class

  4. Use when inheritance hierarchy is clear


5. When to Use an Interface

  1. Classes are unrelated but share some behavior

  2. You want multiple inheritance

  3. Only method signatures are needed, no code implementation

  4. Use when different classes must follow a contract


6. Advantages of Abstract Classes

  1. Code Reuse – Can provide default methods

  2. Organized Hierarchy – Groups related classes

  3. Supports Polymorphism – Child classes can override methods

  4. Partial Implementation – Allows some methods to be abstract, others concrete


7. Advantages of Interfaces

  1. Multiple Inheritance – A class can implement multiple interfaces

  2. Standardized Contracts – Forces classes to implement required methods

  3. Flexibility – Can apply the same interface to unrelated classes

  4. Supports Polymorphism – Objects can be treated as interface type


8. Common Mistakes Beginners Make

  1. Confusing abstract classes with interfaces

  2. Trying to implement abstract methods in the abstract class itself

  3. Forgetting to implement all methods in an interface

  4. Using an abstract class when multiple inheritance is required

  5. Using an interface when default implementation is needed


9. Real-World Analogy

Think of abstract classes and interfaces like vehicles and licenses:

  • Abstract class: Vehicle

    • May have some shared details like fuel_type()

    • Each type of vehicle (Car, Bike) implements move() differently

  • Interface: Drivable

    • Defines only what actions a vehicle must perform (move())

    • Any object (Car, Bike, Truck, Robot) that wants to be “drivable” must implement it


10. Conclusion

  • Abstract Class: partially implemented class, can have both implemented and unimplemented methods, and can store attributes.

  • Interface: fully abstract, defines a contract for other classes to follow.

By mastering the difference between abstract classes and interfaces, beginners can write cleaner, flexible, and reusable programs.

As you continue learning OOP, remember:

Abstract class = partial blueprint, Interface = a contract to follow.