[Newbies] RE: RE: delegation vs inheritance
rstehwien at gmail.com
Mon Aug 6 16:44:04 UTC 2007
> Sorry for the confusion. I'm just using Stack as an example, I don't need to
> use it. I'm just curios of the design choose between delegating (or
> forwarding) vs inheritance. Couldn't Stack inherit from LinkedList and
> specialize it. I'm not questioning the design choose but just trying to
> learn how to make good design decisions.
When trying to decide between inheritance (IS-A) and delegation
(HAS-A) for a new class consider how the behavior of the new class is
related to the old. Should the new class have all the same behavior
(support all the messages) that the old class did?
In the case of a Stack either having an IS-A or HAS-A association with
a LinkedList. Should a stack support all the behavior of a linked
list? The answer is a resounding no. A stack is supposed to have
LIFO (last in first out) behavior while a linked list provides the
ability to alter the list at any point which would break the stack
behavior if exposed. Also the stack only needs the following behavior
push, pop, and count the linked list would provide too broad of an
The IS-A (inheritance) relationship means that everything that the
base class is/provides is also true of the subclass (with some
extensions or behavioral modification).
I'd recommend reading the gang of four's "Design Pattern's" book, it
has a nice discussion of inheritance and delegation.
Hope that helps,
More information about the Beginners