473,406 Members | 2,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Composition vs. Inheritance

Hi Guys,

Can anyone tell What is composition? in detail along with its differences with inheritance in general.
May 16 '07 #1
6 1934
JosAH
11,448 Expert 8TB
Hi Guys,

Can anyone tell What is composition? in detail along with its differences with inheritance in general.
There are two major relationships between classes (objects thereof). The
relationships are "is-a" and "has-a". The first relation can be implemented using
inheritance, while the second relationship is normally implemented using
composition. For the first relationship a Liskov Substitution Principle can
decide whether or not inheritance is the way to go. For the second relationship
the front end class (the composite) acts as a delegator to its member objects.

kind regards,

Jos
May 16 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
Composition is the HAS-A form of class relationships. This mean you will have an object inside your class:
Expand|Select|Wrap|Line Numbers
  1. class Car
  2. {
  3.     private:
  4.        Vechicle v;
  5. };
  6.  
You will now need a Car method to access the Vehicle object.

Inheritance is the IS-A form of class realtionships. As in a Car IS-A Vechicle. Here the Vehicle methods are inherited and you are not required to have a Car method to call a Vechicle method:
Expand|Select|Wrap|Line Numbers
  1. class Car : public Vehicle
  2. {
  3.  
  4. };
  5.  
So, composition is exactly the same as:
Expand|Select|Wrap|Line Numbers
  1. class Car : private Vehicle
  2. {
  3.  
  4. };
  5.  
because, again, the Vechicle is private so you need a Car method to call a Vechicle method. Private inheritantace is often called implementation inheritance since you get the Vehicle as a base class but the Vehicle methods are not available to Car users, that is, the Vechicle implementation has been hidden by the Car.
May 16 '07 #3
AdrianH
1,251 Expert 1GB
There are two major relationships between classes (objects thereof). The
relationships are "is-a" and "has-a". The first relation can be implemented using
inheritance, while the second relationship is normally implemented using
composition. For the first relationship a Liskov Substitution Principle can
decide whether or not inheritance is the way to go. For the second relationship
the front end class (the composite) acts as a delegator to its member objects.

kind regards,

Jos
It should be noted that although "has-a" can be implemented using compositions, it can also be implemented through aggregation.


Adrian
May 16 '07 #4
AdrianH
1,251 Expert 1GB
Composition is the HAS-A form of class relationships. This mean you will have an object inside your class:
Expand|Select|Wrap|Line Numbers
  1. class Car
  2. {
  3.     private:
  4.        Vechicle v;
  5. };
  6.  
You will now need a Car method to access the Vehicle object.

Inheritance is the IS-A form of class realtionships. As in a Car IS-A Vechicle. Here the Vehicle methods are inherited and you are not required to have a Car method to call a Vechicle method:
Expand|Select|Wrap|Line Numbers
  1. class Car : public Vehicle
  2. {
  3.  
  4. };
  5.  
So, composition is exactly the same as:
Expand|Select|Wrap|Line Numbers
  1. class Car : private Vehicle
  2. {
  3.  
  4. };
  5.  
because, again, the Vechicle is private so you need a Car method to call a Vechicle method. Private inheritantace is often called implementation inheritance since you get the Vehicle as a base class but the Vehicle methods are not available to Car users, that is, the Vechicle implementation has been hidden by the Car.
Yeah, I hate it when people use implementation inheritance, I find that it is a bastardisation of inheritance. But of course, I also disagree with multiple inheritance as well as it complicates things and can make code unmanageable.

Both of these types (implementation and multiple inheritance) do have some uses, but I find interface and single inheritance are far cleaner.

But going back to the example that weakness gave, I think that replacing Car with an actual specialisation type, say Honda or Honda Civic, would be better, because Car and Vehicle are both fairly generic in description, and though you can say that a Car is a specialisation of Vehicle, it doesn’t give a very pronounced separation between the two.

Just my 2 cents worth.


Adrian
May 16 '07 #5
weaknessforcats
9,208 Expert Mod 8TB
Yeah, I hate it when people use implementation inheritance, I find that it is a bastardisation of inheritance. But of course, I also disagree with multiple inheritance as well as it complicates things and can make code unmanageable.
You need to do this when your interface is not to contain methods outside the design spec. Private inheritance is identical to a private data member.

Multiple inheritance is harder to justify. However, multiple base classes is the way to provide multiple interfaces to your classes. Evem C# and Java have a form of this with the "implements" keyword.

But going back to the example that weakness gave, I think that replacing Car with an actual specialisation type, say Honda or Honda Civic, would be better,
Possibly. Certainly this is a design choice that could be made. There are no right answers here. A single solution can have many designs.
May 16 '07 #6
AdrianH
1,251 Expert 1GB
You need to do this when your interface is not to contain methods outside the design spec. Private inheritance is identical to a private data member.
No, private inheritance is not identical. With private inheritance, you may call a public member function from the privatly inherited class as if it were part of the class doing the inheriting. With a private data member, you need to invoke the member function with the correct object.
Multiple inheritance is harder to justify. However, multiple base classes is the way to provide multiple interfaces to your classes. Evem C# and Java have a form of this with the "implements" keyword.
Hence my comment on interface inheritance.
Possibly. Certainly this is a design choice that could be made. There are no right answers here. A single solution can have many designs.
Of course.


Adrian
May 16 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: preetam | last post by:
Hi, This question is more towards design than towards c++ details. By looking at books on design patterns and various google threads on the same topic, I see that composition is favoured to...
9
by: Code4u | last post by:
My colleagues and I have been discussing techniques for implementing interfaces in C++. We're looking for a mechanism similar to COM's QueryInterface, in which a certain types of objects can be...
1
by: Tony Johansson | last post by:
Hello experts! I know it's easy to override when you have inheritance. But if you instead use object composition having a pointer to a class X. If you want to override when having this object...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
3
by: Gary Wessle | last post by:
class Client { public: Client(); virtual void login()=0; }; Client::Client(){} class Play : public Client { public: Play();
5
by: =?Utf-8?B?Wg==?= | last post by:
Hi, I want to create a class that sends/receives data from the serial port. There is such a class in .NET v 2.0 but what I need is a serial port with some extra features. For example I want my...
2
by: elangobala | last post by:
can anyone suggest me abt composition .and difference between composition and inheritance.
7
by: snewman18 | last post by:
In learning about design patterns, I've seen discussion about using inheritance when an object's relationship to another object is 'is-a' and composition when the relationship is 'has-a'. Since...
2
by: traineeirishprogrammer | last post by:
I am starting to develop applications in the form of 3 tier really (4 tier) architecture at the moment. I am still learning and at the moment I am developing a simple guest book. With the power...
4
by: Pallav singh | last post by:
Hi All i have dout when to use Composition and Inheritance while designing Module in C++ Thanks Pallav
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.