#685 – Inheritance Can Break Encapsulation

Recall that inheritance and encapsulation are two of the core principles of object-oriented programming.  Encapsulation is the idea of hiding implementation details of a class from code that lives outside the class.  Inheritance is the idea that a class can derive from a parent classs, inheriting data and behavior.

Inheritance increases the coupling between the derived and base classes.  Coupling is the degree to which one class depends on the implementation of another.  Changes in the base class are likely to affect the behavior of the derived class.

Inheritance increases coupling because the derived class obtains access to protected data in the parent class and because it can override behavior in the parent class.  This violates the idea of encapsulation, because the child class can see implementation details in the base class.

Advertisements