Binaya Thapa Magar
Binaya Thapa Magar

Reputation: 210

Composition vs. Inheritance

For a UML Class Diagram, if a class A has a composition relationship with another class B, does it work like inheritance? I know that the class A cannot exist without class b but does class A inherit the attributes and methods from class B?

Upvotes: 4

Views: 5699

Answers (3)

Christophe
Christophe

Reputation: 73617

Composition over inheritance

In OO design, a common advice is to prefer composition over inheritance. This might mislead to think that there is a relation between these two different concepts:

  • Inheritance is about a relation between classes.
  • Composition is a about relations between objects of classes.

Even more misleading: the "composition" used in the general OO literature corresponds in general to a simple UML association and is not to be understood as restrictive as the UML "composition".

Inheritance is about generalisation and specialisation

If class D inherits from B, it means that D specialises a more general concept B:

  • It has all the properties and operations of B (without need to repeat them in the diagram),
  • It can have additional properties and operations that are not relevant for B, and
  • It can perform some operations of B differently.
  • Any object of class D is also an object of class B for its full lifetime. This is why we speak of a "Is a" relationship.

Naive example (just for the illustration):

enter image description here

Composition is about association of objects of very different classes

If class B "composes" (has a composition association with) C, it means that the B object can be associated with different C objects over its lifetime. Anf that objects of class C :

  • always belong to a B object that owns it. This is why we sometimes speak of a "has a" relationship;
  • will not survive its owning B object .

Caution: UML composition is about an exclusive ownership. In the OO literature, the term "composition" often refers to weaker associations without any ownership or even exclusivity.

Naive example (just for the illustration):

enter image description here

Upvotes: 12

bruno
bruno

Reputation: 32594

if a class A has a composition relationship with another class B

Lets suppose you mean A <*>--- B or A <*>---> B rather than the reverse (B <*>--- A or B <*>---> A), because this is not very clear in your question.

class A cannot exist without class b

It is not about classes but about instances of these classes, and B can have instances independently of that composition, so the limited life is not for all instances of B.

does it work like inheritance? does class A inherit the attributes and methods from class B ?

No.

A inherits B means A is a B, this is absolutely not the case having A <*>--- B or A <*>---> B or the reverse.

Then independently of the visibility of the operations you cannot apply the operations of A to the instances of B nor the reverse, except of course if you also have an inheritance more that the composition.

About the attributes of B depending on the way the composition is implemented it is possible they are part of the corresponding instance of A, this is the case for instance in C++ not using a pointer for the composition. But anyway that does not means these attributes of B are attributes of A, to access them it is needed to first access of the instance of B though the compositions, and that also supposes an instance of A has the right to directly access these attributes of B, so they are typically public.

Note also you can have A<*>--->B<*>--->A etc and fortunately that does implies A inherits B and B inherits A which is impossible

Upvotes: 0

qwerty_so
qwerty_so

Reputation: 36333

Inheritance and composition are two different concepts. If they were the same there would not be the need to have different notation.

  • Compostion is about lifetime of objects. Composed elements will be destroyed the moment the owner is destroyed. But each instance can (and likely will) be something completely different than the composing object. The well known example is a car: When you shredder a car its composed wheels will be shreddered as well. Only if you detach them beforehand they can live on.

  • Inheritance means that instances will have operations and attributes on their own defined in the parent class. This works in each single instance. There's no good real worl example for inheritance (genes work different and inherited assets also don't help here).

Upvotes: 0

Related Questions