#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.

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

3 Responses to #685 – Inheritance Can Break Encapsulation

  1. Pingback: #686 – Inheritance vs. Containment « 2,000 Things You Should Know About C#

  2. Pingback: #751 – Inheritance Only Partially Preserves Encapsulation « 2,000 Things You Should Know About C#

  3. Ivan says:

    Hi Sean,

    I am a novice in C# and have a question on this article.

    “This violates the idea of encapsulation, because the child class can see implementation details in the base class.”

    I do not quite understand the above. Why does the child class can see the implementation details in the base class ? I think a child class can only see the public/protected signatures of its parent class.

    Thanks
    Ivan

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s