in reply to Inheritance vs Delegation: pros and cons
My advice would be to only use inheritance if your subclass really has an is-a relationship with the parent class.
Can you swap your class in when somebody else is using the parent class? If not, it's not an is-a relationship.
Thinking about classes with Design by Contract in mind often helps. A subclass should:
If this isn't true then inheritance can rapidly become a burden. You end up with a deep class hierarchy where the sub-sub-sub-class does something completely different from original parent class.
Concerns aren't separated in the code, maintenance becomes a nightmare, and you rapidly create a big ball of mud.
I'm not anti-inheritance (hell, I even think multiple inheritance is a good thing :-) but it should only be used for is-a relationships. Anything else is asking for trouble.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Inheritance vs Delegation: pros and cons
by dragonchild (Archbishop) on Jul 28, 2003 at 15:38 UTC | |
by hakkr (Chaplain) on Jul 28, 2003 at 15:58 UTC | |
by adrianh (Chancellor) on Jul 29, 2003 at 09:31 UTC |