As title says, the meaning of both eludes me.
-
possible duplicate of [Difference between Inheritance and Composition](http://stackoverflow.com/questions/2399544/difference-between-inheritance-and-composition) – nawfal Oct 03 '13 at 10:45
-
Watch this talk and you will understand: https://youtu.be/29MAL8pJImQ?t=1380 – EpicPandaForce Jun 18 '20 at 10:09
8 Answers
Inheritance expresses a is-a
relationship, while composition expresses a has-a
relationship between the two classes.
An example for composition is a polygon. It has a ordered sequence of Points. In C++ terms:
struct Polygon {
std::vector<Point> points;
};
While an logic_error
is a exception
:
struct logic_error : public exception {
};
-
1Speak after me: "A Mercedes ___is___ an automobile. It ___has___ four wheels." It doesn't make sense the other way around. – sbi Sep 22 '11 at 10:11
-
@sbi I tried so hard not to use any of the Dog, DogOwner or Automobile examples and simply took a simplified version of what I was working with currently. You ruined it :( – pmr Sep 22 '11 at 17:11
-
-
@AlirezaRahmani And a very short and not so constructive comment. Please share your insight and consider reading https://en.wikipedia.org/wiki/Is-a and https://en.wikipedia.org/wiki/Has-a – pmr Jan 12 '18 at 13:25
Just google Inheritance vs Composition you'll get alot of results.
Using java as an example
public class Banana extends Fruit{ //<---Inheritance Banana is-a Fruit
private Peel bananaPeel; //<--Composition banana has a Peel
public Peel getPeel(){
return bananaPeel;
}
}

- 370
- 1
- 10
As pmr pointed out, inheritence is a is-a
relationship, composition is a has-a
relationship.
Composition is usually used for wrapping classes and to express relationships between classes that contain one another.
Inheritance is used for polymorphism, where you have a base class and you want to extend or change its functionality.

- 253,575
- 64
- 457
- 625
Inheritance means inheriting something from a parent. For example, you may inherit your mother's eyes or inherit your father's build.
It means deriving properties, characteristics and behaviors from a parent class. So you can parent.walk(), parent.sleep(), parent.sleep() or whatever.
Containership or maybe composition is a bit hard to explain.
Or maybe a car has a brake. The car is composed of a brake. But the brake isn't inheriting from a brake..different concepts. I know very weird explanation..but that's how much I can do I guess.
Let's look at this code:
class Parent
{
public :
void sleep() ; void eat() ; void walk() ;
string eyeColor; int height ;
};
class Child: public Parent
{
}
So the Child class can inherit the functions and attributes of the Parent but of course it may have the eye color of the mother or father.. Even if the childs' attributes are different it can still do whatever the Parent can do.
Now composition is another thing. A Child can have a toy or a Parent can have a child. So I could do:
class Toy
{
string model ;
};
class Child
{
Toy transformersToy ;
};
So the Child has the transformers toy now.. but does Child inherit the transformersToy.model attribute? No, because it isn't inheriting.

- 10,907
- 4
- 48
- 72
-
Sigh, why do so many people complete "idiotic" examples for inheritance? Your Parents can not play with Toys now, our Children can not grow up an become Parents themselves etc. et.c ;D – Angel O'Sphere Sep 23 '11 at 13:07
inheritance is the dynamic polymorphism, that these days change its functionality to a technic of a code reuse. but the composition is a technic for ensuring capability of implementation.

- 2,727
- 2
- 32
- 33
One main point I see is the ownership on the objects. Inheritance doesnt own/give any thing it just gives the characteristics of the base class. While Composition gives the owner ship to the created object.
In the first example polygon has a vector of points. So polygon owns/contains points in it. While in inheritance you can have/use/extend the existing characteristics of the base class.

- 3
- 1
inheritance is a relationship between classes, containership is relationship between instance of classes.

- 6,495
- 10
- 50
- 92
In aspect of memory management there is no difference, the Base class instance comes in the beginning of the DER class, so their addresses are equal. The difference is the type of relationship between them: -in inheritance we have "is-a" -in composition we have "has-a" as mentioned before.

- 23
- 4