Browse Java Design Patterns & Enterprise Application Architecture

Decorator vs. Inheritance

Compare Decorator with inheritance in Java and choose the one that matches variation shape, optional behavior, and runtime composition needs.

Decorator and inheritance both extend behavior, but they do it in very different ways.

Inheritance

Inheritance is a good fit when:

  • the variation is fundamental to the type itself
  • the behavior should be fixed by the subtype
  • the hierarchy remains understandable over time

Decorator

Decorator is a good fit when:

  • behavior is optional
  • behavior should be combined in different ways
  • wrapping order matters
  • the same base implementation may appear with different added concerns

Java Rule Of Thumb

If the change is “this is a different kind of thing,” inheritance may be appropriate.

If the change is “this thing should also do X around the same contract,” decorator is often better.

Failure Mode

Teams sometimes use inheritance to create many variants such as:

  • LoggingRenderer
  • CachingRenderer
  • LoggingCachingRenderer
  • SecureLoggingCachingRenderer

That is exactly the kind of growth decorator is meant to avoid.

Loading quiz…
Revised on Thursday, April 23, 2026