Composition
noun
say it: “kom-puh-ZISH-un” /ˌkɑmpəˈzɪʃən/
Building a class from parts: it keeps other objects in its fields and uses them.
What is Composition?
Composition means building an object out of other objects. A Car has an Engine and has four Wheels. The parts are usually created and owned by the whole.
public class Car
{
private readonly Engine engine = new();
private readonly Wheel[] wheels = { new(), new(), new(), new() };
public void Start() => engine.Start();
}
In a sentence
- “Every bike has a lock through composition: the lock is a field, not a parent.”
Level: Journeyman · More OOP terms at this level
Updated