Inheritance is not object oriented programming man. Object oriented programming is programming with a simple metaphor: objects collaborating each other by sending messages.
Inheritance of type is a key aspect of polymorphism in type-based OO (of course, you can have typeless OO as well).
Inheritance of behaviour is a syntactic shortcut for composition by delegation where there is a “parent” of the same type to delegate to.
Mixins are another syntactic shortcut for composition by delegation.
Avoiding inheritance where it makes sense is silly. Cramming it in where it doesn’t is sillier.
gaboto
Inheritance is neither good nor bad. It is just a mechanism to share behavior between objects. It could be great if we could have different mechanisms to do so…
Take a look to the self language and environment
>> Gaboto
Totally agree with you. I find that inheritance is the least useful (but unfortunately for many others, the most common) way of sharing behaviour between objects. I find it not only ties objects to each others’ lifecycles as well as coupling them far too close for my liking.
>> Csharptest
I find developers who haven’t done much TDD or much Dependency Injection automatically reach towards extending an object, simply to share behaviour (without really thinking about the is-a vs the has-a relationship). I used to be one of them. These days, I find that reuse via composition is a much more elegant way of sharing behaviour.
>> Robert
I disagree that inheritance of behaviour is syntactic shortcut. Two objects of the same hierarchy are bound much closer together than just through code use. Their entire object lifecycle is entwined which makes it difficult to separate them at a later stage. I agree that avoiding inheritance where it makes sense is silly. I just find it doesn’t make sense a lot of the time 🙂
Dale
At uni (ahhh…. the good old days) I had 2 modules explicitly on OO. Begining OO and Advanced OO. 90% of Advanced OO was related to inheritance. Vague memories of drawing out a sequnce diagram for the destruction of a crazy c++ inheritance heirarchy.
I weep for our industry.
Patrick S
You should not inherit for reusing code, but because there is an inhertiance relationship. Sounds like writing things twice more often, but no, that actually works.
If you feel like having to inherit just to reuse an implementation, 95% of the time your model is simply wrong.
Agreed that ‘Object Oriented’ development does not necessitate the use of inheritance nor is inheritance always the best approach. There are cases when a descent OO design includes inheritance/derivations, just as there are valid uses of multiple inheritance. These can tend to be misused and abused by less tenured developers. I have to agree with gaboto:
“Inheritance is neither good nor bad. It is just a mechanism to share behavior between objects.”
I rate it akin to giving a two-year-old a hammer. He’s far more likely to hurt himself and others than to accomplish something useful. Still we all have to learn and grow and we all started life being dangerous with both code and hammers alike.
consumer
I felt that too, but then I understood that inheritance is just a *tool*, which works poorly in most cases, but in some cases it may work extremely well. So, use the right tool for the job. Cheers.
matt mcknight
In many cases, I use inheritance (in Java) to avoid creating too many interfaces. I use it far less in Ruby, because “quack quack quack” seems to work pretty well.
Ben
I’m an OO hater too. The best design pattern for reuse – copy n paste.
A book for Tech Leads, from Tech Leads. Discover how more than 35 Tech Leads find the delicate balance between the technical and non-technical worlds. Discover the challenges a Tech Lead faces and how to overcome them. You may be surprised by the lessons they have to share.
Inheritance is not object oriented programming man. Object oriented programming is programming with a simple metaphor: objects collaborating each other by sending messages.
huh? ok, you have my curiosity up… care to explain?
Inheritance of type is a key aspect of polymorphism in type-based OO (of course, you can have typeless OO as well).
Inheritance of behaviour is a syntactic shortcut for composition by delegation where there is a “parent” of the same type to delegate to.
Mixins are another syntactic shortcut for composition by delegation.
Avoiding inheritance where it makes sense is silly. Cramming it in where it doesn’t is sillier.
Inheritance is neither good nor bad. It is just a mechanism to share behavior between objects. It could be great if we could have different mechanisms to do so…
Take a look to the self language and environment
What?
Who said that to you?
Do I have to mobilize the family?
🙂
>> Gaboto
Totally agree with you. I find that inheritance is the least useful (but unfortunately for many others, the most common) way of sharing behaviour between objects. I find it not only ties objects to each others’ lifecycles as well as coupling them far too close for my liking.
>> Csharptest
I find developers who haven’t done much TDD or much Dependency Injection automatically reach towards extending an object, simply to share behaviour (without really thinking about the is-a vs the has-a relationship). I used to be one of them. These days, I find that reuse via composition is a much more elegant way of sharing behaviour.
>> Robert
I disagree that inheritance of behaviour is syntactic shortcut. Two objects of the same hierarchy are bound much closer together than just through code use. Their entire object lifecycle is entwined which makes it difficult to separate them at a later stage. I agree that avoiding inheritance where it makes sense is silly. I just find it doesn’t make sense a lot of the time 🙂
At uni (ahhh…. the good old days) I had 2 modules explicitly on OO. Begining OO and Advanced OO. 90% of Advanced OO was related to inheritance. Vague memories of drawing out a sequnce diagram for the destruction of a crazy c++ inheritance heirarchy.
I weep for our industry.
You should not inherit for reusing code, but because there is an inhertiance relationship. Sounds like writing things twice more often, but no, that actually works.
If you feel like having to inherit just to reuse an implementation, 95% of the time your model is simply wrong.
Patrick,
Agreed that ‘Object Oriented’ development does not necessitate the use of inheritance nor is inheritance always the best approach. There are cases when a descent OO design includes inheritance/derivations, just as there are valid uses of multiple inheritance. These can tend to be misused and abused by less tenured developers. I have to agree with gaboto:
“Inheritance is neither good nor bad. It is just a mechanism to share behavior between objects.”
I rate it akin to giving a two-year-old a hammer. He’s far more likely to hurt himself and others than to accomplish something useful. Still we all have to learn and grow and we all started life being dangerous with both code and hammers alike.
I felt that too, but then I understood that inheritance is just a *tool*, which works poorly in most cases, but in some cases it may work extremely well. So, use the right tool for the job. Cheers.
In many cases, I use inheritance (in Java) to avoid creating too many interfaces. I use it far less in Ruby, because “quack quack quack” seems to work pretty well.
I’m an OO hater too. The best design pattern for reuse – copy n paste.