Reputation: 210
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
Reputation: 73617
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:
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".
If class D
inherits from B
, it means that D
specialises a more general concept B
:
B
(without need to repeat them in the diagram), B
, andB
differently. 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):
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
:
B
object that owns it. This is why we sometimes speak of a "has a" relationship; 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):
Upvotes: 12
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
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