Using composition as an alternative design approach
Composition is another popular concept in OOP that is again somewhat relevant to encapsulation. In simple words, composition means to include one or more objects inside an object to form a real-world object. A class that includes other class objects is called a composite class, and the classes whose objects are included in a composite class are known as component classes. In the following screenshot, we show an example of a composite class that has three component class objects, A, B, and C:
Composition is considered an alternative approach to inheritance. Both design approaches are meant to establish a relationship between objects. In the case of inheritance, the objects are tightly coupled because any changes in parent classes can break the code in child classes. On the other hand, the objects are loosely coupled in...