Java · Object-Oriented Java

Abstract Classes and Interfaces

Abstract classes and interfaces define what types must do. Choosing well keeps code flexible.

Intermediate 5 min Lesson 15 of 31Examples target Java 21 LTS (they also run on Java 25).
Before you start:Polymorphism
0/31 in Java
On this page · 5 min · Intermediate

In simple words#

An abstract class is a half-built house you finish (shared walls and wiring included). An interface is a certificate listing skills — any class with the skills can hold it, and a class can hold many certificates.

abstract class
Can’t be instantiated; may hold state and code.
interface
A contract; a class can implement many.
default method
Interface method with a body.

Learning objectives#

  • Write abstract classes
  • Write and implement interfaces
  • Use default methods
  • Choose between them

Explanation#

abstract class: may have fields, constructors, concrete and abstract methods. A class extends only one.

interface: methods are abstract by default; can have default and static methods; constants only. A class implements many.

Use an interface for a capability (Comparable, Payable, Notifier). Use an abstract class for a shared base with state.

Most Java frameworks (Spring) are built around interfaces.

Code example: Notifier interface + abstract base#

JavaNotifier interface + abstract base Illustrative
interface Notifier {
    void send(String to, String message);
    default void sendAll(java.util.List<String> recipients, String message) {
        recipients.forEach(r -> send(r, message));
    }
}


abstract class LoggingNotifier implements Notifier {
    private int sent;
    protected void log(String to) { sent++; System.out.println("[" + sent + "] sent to " + to); }
}


class SmsNotifier extends LoggingNotifier {
    public void send(String to, String message) {
        // call SMS provider here
        log(to);
    }
}


public class App {
    public static void main(String[] args) {
        Notifier n = new SmsNotifier();
        n.sendAll(java.util.List.of("+91 98xxxxxx01", "+91 98xxxxxx02"), "Your order has shipped");
    }
}
Expected output
[1] sent to +91 98xxxxxx01
[2] sent to +91 98xxxxxx02

This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.

Step-by-step explanation#

  1. The interface defines send and a default sendAll.
  2. The abstract class adds shared logging state.
  3. SmsNotifier only implements send.
  4. Callers depend on the Notifier interface.

Real-world use case#

Spring services depend on interfaces such as PaymentGateway, so test versions can replace real ones.

Common mistakes#

Abstract class when you need multiple capabilities

A class can extend only one. Use interfaces for capabilities.

Huge interfaces

Split them into small, focused contracts.

Best practices#

  • Program to interfaces
  • Small, focused interfaces
  • Abstract classes for shared state
  • Default methods for convenience only

Remember this#

Interface = can-do contract (many).

Abstract class = shared base (one).

Depend on interfaces.

Practice exercise#

Your task: Create an EmailNotifier that implements Notifier and prints "Email to …".

Show solution
Java Illustrative
class EmailNotifier implements Notifier {
    public void send(String to, String message) {
        System.out.println("Email to " + to + ": " + message);
    }
}

Mini quiz#

Abstract Classes and Interfaces quizMultiple choice Question 1 of 1

Which can a class implement many of?

Score so far: 0/0

Want more? Practise all Java MCQs · Java interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.