How would you implement the Decorator pattern in Java?

The Decorator pattern is implemented by having a decorator class implement the same interface as the object it wraps, hold a reference to that wrapped object, and add behavior before or after delegating to it. This lets you layer new behavior onto an object without modifying its class or using inheritance.

Key Points

Example

A InputStream chain like new BufferedInputStream(new FileInputStream("file.txt")) is a real-world use of Decorator baked into the Java standard library.

interface Coffee {
    double cost();
}

class SimpleCoffee implements Coffee {
    public double cost() { return 2.0; }
}

abstract class CoffeeDecorator implements Coffee {
    protected Coffee wrapped;
    CoffeeDecorator(Coffee wrapped) { this.wrapped = wrapped; }
}

class MilkDecorator extends CoffeeDecorator {
    MilkDecorator(Coffee wrapped) { super(wrapped); }
    public double cost() { return wrapped.cost() + 0.5; }
}

Interview Tip

A concise interview answer is

I make the decorator implement the same interface as the object it wraps, store a reference to the wrapped object, and add behavior before or after delegating the call. Because a decorator is itself a Component, I can stack several of them to combine behaviors, which is exactly how Java's InputStream wrappers work.

Last updated: 16 July 2026

Previous question: What is the Decorator pattern and how does it differ from inheritance?

Next question: What are the advantages of using the Decorator pattern for extending behavior?

More Java Design Patterns interview questions

All Java Design Patterns questions