Java

Abstract Class vs Interface (After Java 8)

By Utility Zone · 2026-02-16T17:09:43.94802

Overview

After Java 8, interfaces became more powerful with the introduction of: - Default methods - Static methods - (Java 9+) Private methods

However, abstract classes and interfaces still serve different design purposes.


1. Abstract Class

Definition

An abstract class: - Cannot be instantiated - Can contain both abstract and concrete methods - Can have constructors - Can maintain state (instance variables)

Example

abstract class Vehicle {

    String brand;  // instance state allowed

    Vehicle(String brand) {
        this.brand = brand;
    }

    abstract void start();

    void stop() {
        System.out.println("Stopping vehicle");
    }
}

Key Features

  • Can define state
  • Can define constructors
  • Supports method implementation
  • Supports access modifiers (private, protected, public)
  • Single inheritance only

2. Interface (After Java 8)

Definition

An interface defines a contract that classes must follow. After Java 8, it can also provide default behavior.

Example

interface Vehicle {

    void start();  // implicitly public abstract

    default void stop() {
        System.out.println("Stopping vehicle");
    }

    static void utility() {
        System.out.println("Utility method");
    }
}

Key Features

  • No constructors
  • No instance state (only constants)
  • Supports default methods
  • Supports static methods
  • Supports multiple inheritance

Major Differences

Feature Abstract Class Interface (Java 8+)


Constructors Yes No Instance variables Yes No (only constants) Method types Abstract + Concrete Abstract + Default + Static Multiple inheritance No Yes Access modifiers Any Methods are public by default State Can maintain state Cannot maintain instance state Use case Base class Contract / capability


IS-A vs CAN-DO

Abstract Class → IS-A Relationship

Example: Dog IS-A Animal

Interface → CAN-DO Relationship

interface Flyable {
    void fly();
}

class Bird implements Flyable {
    public void fly() {
        System.out.println("Flying");
    }
}

Bird CAN-DO fly.


Diamond Problem Handling

If two interfaces define the same default method:

interface A {
    default void show() {}
}

interface B {
    default void show() {}
}

class C implements A, B {
    public void show() {
        A.super.show();
    }
}

Java forces overriding to avoid ambiguity.


When to Use Abstract Class

  • When you need shared state
  • When you need constructors
  • When you want partial implementation
  • Template Method pattern use cases

When to Use Interface

  • When defining capability/contract
  • When supporting multiple inheritance
  • When designing loosely coupled systems
  • Strategy pattern, Comparable, Runnable, etc.

Important Interview Insights

  • Prefer interfaces for flexibility
  • Use abstract class when shared state or base behavior is needed
  • Interfaces support multiple inheritance; abstract classes do not
  • No significant runtime performance difference

Summary

Abstract Class → Base implementation + shared state
Interface (Java 8+) → Contract + default behavior + multiple inheritance

Choose based on design need, not syntax convenience.