?
Solved

when to use inheritance and composition

Posted on 2014-12-30
3
Medium Priority
?
190 Views
Last Modified: 2015-01-02
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
0
Comment
Question by:Rohit Bajaj
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 34

Assisted Solution

by:it_saige
it_saige earned 600 total points
ID: 40523713
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
 
LVL 32

Accepted Solution

by:
phoffric earned 800 total points
ID: 40523884
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
 
LVL 28

Assisted Solution

by:dpearson
dpearson earned 600 total points
ID: 40524760
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

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
This video teaches viewers about errors in exception handling.
Introduction to Processes
Suggested Courses
Course of the Month14 days, 7 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question