Decorator Pattern vs Inheritance with examples

Viewed 10999

I've been experimenting with the decorator pattern to extend functionality of code you do not want to touch for example and I see how to implement it however I am now unsure why you don't just inherit from the original class and extend that way.

I have read that the decorator pattern allows you to add functionality at runtime whereas inheritance means its there at compile time.

I don't understand this.

Could someone explain this, provide examples and explain when its better to use decorator vs inheritance.

Thanks

4 Answers
Related