Solved

when to use inheritance and composition

Posted on 2014-12-30
3
184 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 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 28

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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

717 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