You are currently viewing Abstract Classes and Interfaces in Java

Abstract Classes and Interfaces in Java

Introduction

In Java, both abstract classes and interfaces allow you to define contracts and shared behavior for other classes. But when should you use one over the other?

This article explores the differences, use cases, and practical examples of abstract classes and interfaces to help you choose the right approach for your design.


1. What is an Interface?

An interface defines a contract. It contains method signatures that implementing classes must provide.

interface Animal {
    void eat();
    void sleep();
}

A class implements an interface like this:

class Dog implements Animal {
    public void eat() {
        System.out.println("Dog eats.");
    }

    public void sleep() {
        System.out.println("Dog sleeps.");
    }
}

Since Java 8, interfaces can also contain:

  • default methods with implementation
  • static methods
  • private methods (from Java 9+)

2. What is an Abstract Class?

An abstract class is a class that cannot be instantiated and may contain:

  • Abstract methods (no body)
  • Concrete methods (with body)
  • Fields and constructors
abstract class Vehicle {
    String brand;

    abstract void start();

    void fuelUp() {
        System.out.println("Filling fuel");
    }
}

A subclass must provide implementations for all abstract methods:

class Car extends Vehicle {
    void start() {
        System.out.println("Car is starting...");
    }
}

3. Key Differences

FeatureInterfaceAbstract Class
Inheritance typeImplementsExtends
Multiple inheritanceYes (can implement many interfaces)No (can extend only one class)
ConstructorsNot allowedAllowed
Fieldspublic static final onlyAny access modifier
Method body alloweddefault, static (Java 8+)Yes (concrete + abstract methods)
Use caseCapability or contractShared base behavior + state

4. When to Use What

  • Use interfaces when:
    • You want to define a capability or contract (e.g., Comparable, Runnable)
    • You need to support multiple inheritance
  • Use abstract classes when:
    • You want to share common state or logic
    • You want to provide a partially implemented blueprint

5. Real-World Example: Notification System

Let’s say you want to define a framework for sending notifications.

Option 1: Interface

interface Notifier {
    void send(String message);
}

Used when each implementation is completely independent (Email, SMS, Slack…).

Option 2: Abstract class

abstract class BaseNotifier {
    void log(String message) {
        System.out.println("LOG: " + message);
    }

    abstract void send(String message);
}

Used when you want to enforce logging or setup logic in all notifiers.


Conclusion

Both abstract classes and interfaces are powerful tools in Java. Use them deliberately:

Understanding the distinction makes your code more expressive, reusable, and aligned with object-oriented best practices.

You can find the complete code of this article here in GitHub.

Noel Kamphoa

Experienced software engineer with expertise in Telecom, Payroll, and Banking. Now Senior Software Engineer at Societe Generale Paris.