Introduce Inheritance & Object Composition

Two important aspects of the Open-Close-Principle

Itchimonji
Sep 8, 2020 · 6 min read
Image for post
Image for post
Made by Itchimonji

These days for your applications, a decoupled and encapsulated software system is ever more relevant. Reusability, maintenance, care, and sustainability play a major role in our software development projects — they are getting bigger and bigger. People, who try to master these challenges, read Robert C. Martins Clean Code, Clean Coder, or Clean Architecture. Actually, some of them follow his SOLID principles:

So, why should we care about the so called “Inheritance & Object Composition”? Because they are a big part of one of the aforementioned SOLID principles: the Open-Closed-Principle.

„Software entities … should be open for extension, but closed for modification.“

What’s more, inheritance and object composition decide about whether your software application is going to be monolithic or decoupled. We need to understand these concepts to make our software systems cleaner.

What is inheritance?

In short, inheritance describes an “is a”-relationship between 2 participants. For example, the earth is a planet, which is a celestial body. Or a cube is a geometric figure.

Image for post
Image for post
Simple inheritance

Facts about inheritance:

The “Gang of Four” (Design Patterns) describe inheritance as a white-box reuse, because the internals of base classes are visible to their subclasses. And because of the strong linkage between base and subclass, the modification of the base class can affect the subclass, too. So, the base class implementation breaks up the encapsulation of all subclasses — this might be hard to change later.

This relationship is tightly coupled and entails many code risks by changing the superclass. The more a class knows about another class, the more they are coupled. Only inheritance of abstract classes can prevent this.

class Creature {
private isNatural: boolean;
protected size: number;

public beAlive(): void {
console.log('Im alive');
}
constructor (isNatural: boolean) {
this.isNatural = isNatural;
}
}
class Human extends Creature {
protected age: number;
public setSize(value: number): void {
this.size = value;
}
constructor(age: number) {
super(true);
this.age = age;
this.beAlive();
}
}
class Worker extends Human {
constructor(size: number) {
super(38);
this.beAlive();
this.setSize(size);
}
}

What is object composition?

An other technique of code reuse is object composition. In short, it describes a “has a”-relationship between 2 or more participants. For example, a car has got wheels, an engine, windows, doors, and a lot of electronic stuff.

Image for post
Image for post
Object Composition with Dependency Inversion

Facts about object composition:

The “Gang of Four” (Design Patterns) describe Object Composition as black-box reuse, because you can’t see the internal code — only the interface.

This loosely coupled concept and the interface-access allow more flexibility in code design, secure Uncle Bobs Open-Closed-Principle, and lead to encapsulation. Because your existing and working code will not be altered while you can still add new behaviour — you can change components and objects without any code risks and without having to think about code dependencies.

Besides, using object composition is a good method to mock test data.

interface AbstractEngine {
howMuchIsIt(): void;
}
class DieselEngine implements AbstractEngine {
private cycle: string;
private compressionRatio: string;
public howMuchIsIt(): void {
console.log('High initial and maintenance costs');
}
constructor() {
this.cycle = 'Diesel';
this.compressionRatio = 'high';
}
}
class PetrolEngine implements AbstractEngine {
private cycle: string;
private sounds: string;
public howMuchIsIt(): void {
console.log('Low initial and maintenance costs');
}
constructor() {
this.cycle = 'Otto';
this.sounds = 'Brumm brumm..';
}
}
class Car {
public engine: AbstractEngine;
public drive();
}
function buildCar(engine: AbstractEngine): Car {
const result: Car = new Car();
result.engine = engine;
return result;
}
function buildCarWithPetrolEngine(): Car {
const result: Car = buildCar(new PetrolEngine());
result.howMuchIsIt(); // Low initial and maintenance costs
return result;
}
function buildCarWithDieselEngine(): Car {
const result: Car = buildCar(new DieselEngine());
result.howMuchIsIt(); // High initial and maintenance costs
return result;
}

What to prefer: Inheritance or Object Composition?

The “Gang of Four” write in Design Patterns, you should prefer composition over inheritance whenever you can, because of the decoupled principle.

Both have got strengths and weaknesses, but most developers instinctively opt for inheritance. Maybe they learnt it through their education and gained more experience than with composition. Personally, I am using object composition more than inheritance.

What you prefer depends on your problem. Most UI frameworks are based on inheritance (e.g. WinForms in C#). On the other hand most Design Patterns use object composition.

“Program to an interface, not an implementation.”

Object composition in action: Abstract Factory

The abstract factory pattern provides a way to encapsulate a group of individual factories that have a common theme without specifying their concrete classes.

Usage:

Example for creating a theme feature:

Conclusion

These days it’s very important to design our software applications more decoupled and encapsulated. Object composition is a concept to reuse code more flexibly. Inheritance is better known and easier to handle. Also, you can combine both concepts. I started to use object composition more often after reading the book “Design Patterns”. I learnt it is very important to build your software system in an easily maintainable way. We should try to be DRY and reuse our code.

Design Patterns are great and it is fun to code them — object composition is a big part of it. Besides, you learn other coding techniques — for me, I now better understand Angular with its own art (Dependency inversion, observables, states, etc). It’s worth to take a look at them. Try it out!

Thank you and Happy Coding! :)

Itchis Coffee Shop

TypeScript, Angular, Object Composition, Tutorials, UI and much more related to development.

Itchimonji

Written by

Fullstack Software Engineer | Trained Mathematical-Technical Software Developer | Employee at Energy2market

Itchis Coffee Shop

Tiny Blog about Object Composition, Inheritance and Development in TypeScript and Angular. Nice tutorials are also common content. Check it out!

Itchimonji

Written by

Fullstack Software Engineer | Trained Mathematical-Technical Software Developer | Employee at Energy2market

Itchis Coffee Shop

Tiny Blog about Object Composition, Inheritance and Development in TypeScript and Angular. Nice tutorials are also common content. Check it out!

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store