Solved

when to use inheritance and composition

Posted on 2014-12-30
3
170 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 32

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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…
This is about my first experience with programming Arduino.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

706 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

16 Experts available now in Live!

Get 1:1 Help Now