#685 – Inheritance Can Break Encapsulation
October 4, 2012 3 Comments
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.
Pingback: #686 – Inheritance vs. Containment « 2,000 Things You Should Know About C#
Pingback: #751 – Inheritance Only Partially Preserves Encapsulation « 2,000 Things You Should Know About C#
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