Solved

when to use inheritance and composition

Posted on 2014-12-30
3
173 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
3 Comments
 
LVL 33

Assisted Solution

by:it_saige
it_saige earned 150 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 200 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 26

Assisted Solution

by:dpearson
dpearson earned 150 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java SE 8u111  Lot of stuff broke 11 54
ForLoop Example 3 40
xampp tool 12 28
T-SQL:  Sigh---Boy, this is fun.... 12 33
This article will show, step by step, how to integrate R code into a R Sweave document
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

895 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now