TAGS :Viewed: 5 - Published at: a few seconds ago

[ Java Inheritance implemented with Decorator pattern ]

Is it right to think that under the hoods java inheritance
is implemented using Decorator Pattern ?
In my mind inheritance and Decorator Pattern look very similar almost indistinguishable.

EDIT:
I mean, let A be a subclass of B,we can say that in A the keyword superalways refers to the object of type A we are "decorating".

Thanks

Answer 1


I doubt Java implements inheritance under the hood using the Decorator pattern. Relationships between base classes and their extensions are fixed at compile time, whereas relationships between decorators and the objects they decorate can change at run time.

Answer 2


What inheritance and decorator pattern have in common is the fact that they both allow you to change how an object behaves. Also, to be more precise, decorator pattern is implemented by the concept of inheritance.

But the difference:

1.Decorator pattern makes run-time object changes easier:

2.Decorator Pattern makes multiple behavior modifications easier

3.Decorator pattern is not necessary in non-dynamic situations