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?
Classes and objects created through inheritance are tightly coupled because changing the parent or superclass in an inheritance relationship risks breaking your code.
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.
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.
Composition creates a dependency to an interface, so de facto some coupling. But the more generic the interface, the loser the coupling.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With