when to use inheritance and composition

Hi,
I have a class A in which i want to use things from other class B. One way to do it is to extend Class B. Other is to include an object of class B inside A. Which is a better approach and will lead to a good design.

Thanks
Rohit BajajAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

it_saigeDeveloperCommented:
Good design is the key element.  In general, if class A is an extension of class B or class B can be viewed as a base of class A that you use the inheritance route.

Take for example a LunchBasket class, Fruit class, Apple class, Orange class, Sandwich class and Blanket class (simplified example as a Sandwich could contain many other objects that have their own bases such as Spread, Peanut Butter and Jelly, etc.).

The superclass in this case would be LunchBasket as a LunchBasket can contain Fruit, Sandwiches and a Blanket.
The Fruit class would be a base class for Apple and Orange as both an apple and an orange are fruit's but they each can have their own properties (acidity) but can share common properties (size, shape, color, etc.).
Sandwiches and Blankets are not fruit nor are they directly related to each other.

-saige-
0
phoffricCommented:
In addition to it_saige comment, there is a test to help determine whether to use inheritance or composition.

"is a"
"has a"
These are the key phrases to help make the determination.

A dog, or a cat, or a lion "is a" mammal, so dog, cat, and lion can be inherited from mammal (which could have animal as its base class, since a mammal "is an" animal.

A house "has a" room; a house is a container for a room. So, a house class "has a" list of rooms in its class.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dpearsonCommented:
Just to throw one more thought into the mix - I'd say the thinking on this has changed over the years.

If you asked this question 10 years ago many people would have said "either choice is fine depending on the design".

Today though I think the more common answer is "prefer composition unless there's a very clear reason why inheritance is better".

In other words, if it's not clearly a situation where inheritance is warranted, composition will produce the better long term benefits.   (Inheritance is when you use 'extends', composition is when you put a B inside A).

Inheritance produces lots of unintended issues and side-effects (e.g. exposing methods that aren't required in the subclass, providing unintended access to the base class's member variables, producing errors in the subclass when the base class's implementation details are changed etc. etc.).  Composition doesn't suffer from these problems so it's usually best.

Doug
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.