DEV Community

Bhavesh Daswani
Bhavesh Daswani

Posted on

Composition vs Inheritance

Composition vs Inheritance
Inheritance is about what it is while composition is all about what it has or what it's ability is.In Inheritance we pre-define the architecture like parent class is inherited by sub classes and this sub class is inherited by another sub classes and this nesting continues the more nesting we have the more rigid is our code that is more harder to change and some times our code may get fragile that is if we change in parent class it may effect sub class in unexpected manner.For example I have a Base class Human then three sub class young,old,adult then we have inner sub class for each of them which could be male,female.But how this inner classes or inheritance make our code rigid or harder to change in future let's understand this issue with an example.

// we have to represent human relation in oop style
class Human {
constructor(firstName,lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
sleepNow() {
console.log(`${this.firstName} ${this.lastName} is sleeping`)
}
eatNow() {
console.log(`${this.firstName} ${this.lastName} is eating`);
}
}
class Young extends Human {
constructor(firstName,lastName,age) {
super(firstName,lastName);
this.age = age;
}
startStudy() {
console.log(`${this.firstName} is studing`)
}
goToSchool() {
console.log(`${this.firstName} is going to school`)
}
}
class Male extends Young {
constructor(firstName,lastName,age,gender) {
super(firstName,lastName,age);
this.gender = gender;
}
// here all the method of young and human gets also Male does not need some of the method all method are made available to Male class
}
// The main issue with inheritance is that we predefine the whole architecture we have to define the whole chain of Human, Young,Male
// there properties and method this also make tight coupling suppose a method sleepNow has need some change like we want to perform
// prayer before sleep then it will effect the whole chain from where it being called this could be desired result but some time
// this could result in breakdown or unexpected behaviour
// How compose can solve this problem
getSleepAbility(human) {
return Object.assign({},human,{toSleep:()=>{console.log('this provide sleep ability.')}});
}
function man(firstName,lastName,age,gender) {
let man = {
firstName,
lastName,
age,
gender
};
return getSleepAbility(man);
}
// In FP programming we more focus on the what it has or what ability it has so i have created two function function man which get
// ability to sleep from getSleepAbility this way our code is future capable no tight coupling

In class base inheritance i have created three classes Human -> Young -> Male
The method of Young and Human class are available to Male class instance, this is what i mean the inheritance is all about what it is, we are predefine the relationship and there inheritance. Suppose in future we have certain requirement for which we need to change the inheritance flow to Human->Male->Young. Some time it is possible to do change like this, but i am sure that in near future it will break because tight coupling that is Young class depend on Human, Male class depend on Young class will definitely will create issue in future because as a human we cannot predict future, changes will be there so how to resolve this. My preference is composition. Composition focuses on what is the ability, for that to understand i have created two function. A man function return an a instance with sleep ability which is provided by getSleepAbility function. So dividing the requirement into ability and composing required abilities is the answer to tight coupling which is caused by what it is instead of what ability it has.

Top comments (0)