DEV Community

Cover image for Abstract Classes and Interfaces in Java
Noel KAMPHOA
Noel KAMPHOA

Posted on

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();
}
Enter fullscreen mode Exit fullscreen mode

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.");
    }
}
Enter fullscreen mode Exit fullscreen mode

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");
    }
}
Enter fullscreen mode Exit fullscreen mode

A subclass must provide implementations for all abstract methods:

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

3. Key Differences

Feature Interface Abstract Class
Inheritance type Implements Extends
Multiple inheritance Yes (can implement many interfaces) No (can extend only one class)
Constructors Not allowed Allowed
Fields public static final only Any access modifier
Method body allowed default, static (Java 8+) Yes (concrete + abstract methods)
Use case Capability or contract Shared 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);
}
Enter fullscreen mode Exit fullscreen mode

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);
}
Enter fullscreen mode Exit fullscreen mode

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.

Originally published on my blog at: https://nkamphoa.com/abstract-classes-and-interfaces-in-java/

Top comments (0)