HomeInterview QuestionsWhat does a decorator class do when applied on top…

What does a decorator class do when applied on top of another class?

🟡 Medium Conceptual Fresher level
1Times asked
Apr 2026Last seen
Apr 2026First seen

💡 Model Answer

A decorator class is a structural design pattern that allows you to add new behavior to an existing object dynamically, without modifying its code. It does this by wrapping the original object in a new class that implements the same interface. The decorator forwards all method calls to the wrapped object, optionally adding pre‑ or post‑processing logic. This approach adheres to the Open/Closed Principle: classes remain closed for modification but open for extension. Decorators are useful for cross‑cutting concerns such as logging, caching, or authentication. They also enable runtime composition of features, allowing multiple decorators to be stacked. For example, in Java you might have a Component interface, a concrete ConcreteComponent, and a ComponentDecorator that holds a reference to a Component. Each concrete decorator adds its own behavior before or after delegating to the wrapped component.

This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.

🎤 Get questions like this answered in real-time

Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers — invisible to screen sharing.

Get Assisting AI — Starts at ₹500