Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does inheritance increase coupling?

Tags:

java

c++

oop

I am fairly new to object oriented programming techniques and as i understand, coupling is the biggest obstacle in creating maintainable, reusable code.

When we make several child classes inherit from a base class, don't the child classes become dependent on the base class? What if i have to remove the base class at a later point in time. Wouldn't this break the code in the child classes?

like image 783
Jobin Jose Avatar asked Mar 21 '15 19:03

Jobin Jose


People also ask

Does inheritance create tight coupling?

Classes and objects created through inheritance are tightly coupled because changing the parent or superclass in an inheritance relationship risks breaking your code.

Is inheritance a coupling?

Inheritance coupling refers to the coupling of two classes when one class is a subclass of another. The coupling is through data members that are inherited from a parent class but not re-defined by its subclass. The coupling can be computed by Names, Binary, or by Counts.

Is inheritance loose coupling?

Inheritance is tightly coupled whereas composition is loosely coupled. Let's assume we have below classes with inheritance. For simplicity, we have both the superclass and subclass in a single package.

Does composition increase coupling?

Composition creates a dependency to an interface, so de facto some coupling. But the more generic the interface, the loser the coupling.


1 Answers

Yes. This is referred to as Subclass Coupling:

Describes the relationship between a child and its parent.

Like all coupling, affecting one unit may affect the units that depend on it.

In this case, removing the parent class would affect all it's child classes, as a compiler error would occur on removal of the supertype.

This means the child classes are coupled to the supertype.

like image 182
Vince Avatar answered Sep 22 '22 21:09

Vince