A Principle Of Object Oriented Design (Composition Over Class Inheritance)

Ahmad Kamil Almasyhur
3 min readMar 28, 2019

--

Favor object composition over class inheritance

In object oriented programming, we know about 4 well-known concept of object oriented programming as abstraction, encapsulation, inheritance, and polymorphism. In addition of object oriented programming, that you can use object oriented design as your other concept of programming using object oriented method. One of the concept was, favor object composition over class inheritance, which means that we prefer to use composition instead of inheritance. But, is it always about using composition instead of inheritance?

source: bp.blogpost.com

Object Composition

Object composition requires that the objects being composed have well-defined interfaces. This style of reuse is called black-box reuse, because no internal details of objects are visible.

Object Composition Advantages

Object composition is defined dynamically at run-time through objects acquiring references to other objects. Composition requires objects to respect each others’ interfaces, which in turn requires carefully designed interfaces that don’t stop you from using one object with many others. Favoring object composition over class inheritance helps you keep each class encapsulated and focused on one task. Your classes and class hierarchies will remain small and will be less likely to grow into unmanageable monsters. Other advantages is composition is much more flexible than Inheritance. Because you cannot changed extending class at runtime, but with composition, you just define a `Type` which you want to use that can hold its different implementation.

Object Composition Disadvantages

A design based on object composition will have more objects (if fewer classes), and the system’s behavior will depend on their interrelationships instead of being defined in one class.

Class Inheritance

Class inheritance lets you define the implementation of one class in terms of another’s. Reuse by subclassing is often referred to as white-box reuse. The term “white-box” refers to visibility: With inheritance, the internals of parent classes are often visible to subclasses.

Class Inheritance Advantages

Class inheritance is defined statically at compile-time and is straightforward to use, since it’s supported directly by the programming language. Class inheritance also makes it easier to modify the implementation being reused. When a subclass overrides some but not all operations, it can affect the operations it inherits as well, assuming they call the overridden operations.

Class Inheritance Disadvantages

Class inheritance has some disadvantages, too. First, you can’t change the implementations inherited from parent classes at run-time, because inheritance is defined at compile-time. Second, and generally worse, parent classes often define at least part of their subclasses’ physical representation. Third, It’s being hard to do unit test, because you cannot mock parent class.

Suggestion

When to use Inheritance

Only use inheritance only for the sub-type of of your class type, or your subtype is-a relationship with the super class. Lets say about mammals, tritylodontidae and mammaliaformes, which mammal and tritylodontidae are a subtype of mammaliaformes. So you can use inheritance, so then when you need to switch that, you can do that easily (Liskov Substitution Principle).

When to use Composition

You are able to use composition at many condition when you have has-a relationship with other object. Says that you have a car, and you have the wheels, you may use has-a relationship which a car has a wheel (or the car has 4 wheels). So you can use KISS Principle, which is keep it simple and stupid (class/object).

Source and Further Read:

Design Pattern Book: Elements of Reusable Object-Oriented Software

--

--