Browse Java Design Patterns & Enterprise Application Architecture

Class Adapter vs. Object Adapter

Compare class and object adapters in Java, with emphasis on why composition-based object adapters are usually the safer default.

The classic Adapter pattern has two variants:

  • class adapter, which relies on inheritance
  • object adapter, which relies on composition

In Java, object adapter is usually the better default.

Class Adapter

A class adapter extends the adaptee and implements the target interface:

 1public final class LegacyGatewayClassAdapter
 2        extends LegacyGateway
 3        implements PaymentProcessor {
 4
 5    @Override
 6    public Receipt charge(Money amount, String token) {
 7        LegacyResponse response =
 8            makePayment(amount.toMinorUnits(), token);
 9        return new Receipt(response.reference(), response.approved());
10    }
11}

This can be compact, but it comes with trade-offs:

  • it only works when inheritance from the adaptee is allowed
  • it couples the adapter to one concrete implementation
  • it is harder to swap or test with a different adaptee instance

Object Adapter

An object adapter receives the adaptee as a collaborator:

 1public final class LegacyGatewayObjectAdapter
 2        implements PaymentProcessor {
 3    private final LegacyGateway gateway;
 4
 5    public LegacyGatewayObjectAdapter(LegacyGateway gateway) {
 6        this.gateway = gateway;
 7    }
 8
 9    @Override
10    public Receipt charge(Money amount, String token) {
11        LegacyResponse response =
12            gateway.makePayment(amount.toMinorUnits(), token);
13        return new Receipt(response.reference(), response.approved());
14    }
15}

This fits Java better because composition is more flexible than inheritance and plays well with testing, DI, and alternate implementations.

Which One Should You Choose?

Use a class adapter only when:

  • inheritance is allowed and safe
  • the adaptee really is the only implementation you care about
  • you benefit from direct access to protected behavior on the adaptee

Use an object adapter in most application code.

Design Review Rule

If the adapter is part of an application boundary, prefer object adapter. If it is a narrow internal convenience wrapper and inheritance genuinely simplifies the design, class adapter may be acceptable.

Loading quiz…
Revised on Thursday, April 23, 2026