Is Inheritance Bad?
I hear many voices claiming that Inheritance is bad and recommending to use Composition instead. Their arguments have sense - I saw many examples on how to incorrectly use inheritance.
I would argue on one argument which implicates a tight coupling between a parent and extending class. Let's take classic Vehicle and Car extending Vehicle example. Can we say Car is close coupled with Vehicle? No, because Car is a Vehicle!
What do you think?
I would argue on one argument which implicates a tight coupling between a parent and extending class. Let's take classic Vehicle and Car extending Vehicle example. Can we say Car is close coupled with Vehicle? No, because Car is a Vehicle!
What do you think?

8 Comments:
My opinion is that inheritance is not bad if it is shallow. Once you start having 3 or more ancestors, the problem is better solved with composition. It allows more flexibility.
You sure have a lot of interesting things you write in your blog. I am not sure what to think about something being in herited. I guess it depends on what the time is that is being passed down from generation to generation and what the person who gets that item does with it.
Inheritance vs composition is an implementation decision, not a design decision.
If you follow strong separation between implementation and declaration, inheritance is a fast way to get the job done without compromising future evolutions, and without duplicating code.
Thierry,
Thanks for your comment.
Take a classic example of object relation question. Should we have Employee inherited from Person, or should we use composition here? Is it a question of implementation, or a question of design?
For me, there is very little barrier between design phase and "implementation" phase. Actually both design and coding are implementation of our requirements. The difference we have is that we deal with higher-level decisions during design, and lower-level decisions during implementation. We meet with inheritance vs composition in both design (as in example earlier) and implementation.
How do you see the strong separation between implementation and "declaration" could be accomplished, and what it results to?
following dkrukovsky, Employee is just a 'facet' or 'aspect'a Person has, it's not a new kind of Person. Languages I know dont have clear semantics of expressing this nature, what can be done so far is having a Person Interfaces which both Employee and Person implement, and having a Employee composed by a Person.
The biggest issue I have with Inheritance is that it is overused.
if the relationship can accurately be described as a "is-a" relationship, then it's probably a good reason to use inheritance. But, many time developers use inheritance because 2 objects have 1 thing in common (and nothing else in common), and they don't have enough tools in their toolbox to solve the problem any other way (like delegates, interfaces, and patterns).
Roberto! Bill Venners described well-designed Person / Employee relations at
http://www.artima.com/designtechniques/compoinhP.html
Youth is not wow gold a time of life;world of warcraft gold it is a state of mind; cheap wow gold it is not a Maple Story Accounts matter of rosy cheeks, red lips and supple knees;mesos it is a matter of the will, a quality of the imagination,wow gold kaufen a vigor of the emotions; it is the freshness wow geld of the deep springs of life.maple story mesos Youth means a tempera-mental predominance of courage over timidity, of the appetite for adventure over the love of ease. This often exists in a man of 60 more than a boy of 20.wow gold farmen Nobody grows old merely by a number of years.maple story money We grow old by deserting our ideals.ms mesos Years may wrinkle the skin, but to give up enthusiasm wrinkles the soul. Worry, fear, self-distrust bows the heart and turns the spring back to dust. Whether 60 or 16, there is in every human being’wow powerleveling s heart the lure of wonder, the unfailing childlike appetite of what’s maple story money next and the joy of the game of living.powerlevel In the center of your heart and my heart there is a wireless station: so long as it receives messages maplestory powerleveling of beauty, hope, cheer,world of warcraft power leveling courage and power from men and from the Infinite
Post a Comment
Links to this post:
Create a Link
<< Home