#751 – Inheritance Only Partially Preserves Encapsulation

It can be argued that inheritance breaks encapsulation, since there is a tight coupling between the parent and child classes and because the child class can override behavior in the parent class.

But inheritance does partially preserve encapsulation, with respect to private members of the base class.

  • An instance of the child class can’t access private members in the base class
  • The implementation of the child class can’t access private members in the base class

Assume that we have a Dog class with a private field barkCount and a Terrier class that derives from Dog.

            Terrier t = new Terrier();

            // ERROR: Dog.barkCount inaccessible due to its protection level
            int i = t.barkCount;

Also:

    public class Terrier : Dog
    {
        public void Test()
        {
            Console.WriteLine("Terrier " + Name);

            // ERROR: Dog.barkCount inaccessible due to its protection level
            int i = this.barkCount;
        }
    }

#697 – Encapsulation is Managed through the Use of Access Modifiers

Encapsulation is one of the core principles of object-oriented programming.  Encapsulation is the idea of hiding implementation details of a class from the users of that class and only exposing a public interface.

When you author a class in C#, you can decide which class members (e.g. properties, fields, methods, events, etc) are visible to users of the class.  You do this by using access modifiers to indicate the accessibility of each class member.

Making some class members private allows hiding data from the users of the class, which supports the principle of encapsulation.  Encapsulation is also achieved by preventing access to methods whose use are internal to the code in the class.

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

#251 – Class Properties Support the Principle of Encapsulation

Encapsulation is one of the core principles of object-oriented programming.  Encapsulation is the idea of hiding implementation details of a class from the users of that class and only exposing a public interface.

Class properties in C# support the idea of encapsulation.  Client code that wants to read or write a property has access only to its public interface–the data type of the property and the knowledge that there is a get or a set accessor (or both) for that property.

Client code that reads or writes a property has no information about:

  • How the class actually stores the property data
  • The implementation of the get accessor–how data is retrieved when a client reads the property value
  • The implementation of the set accessor–how data is stored when a client writes the property value

#229 – The Core Principles of Object-Oriented Programming

As an object-oriented language, C# supports the three core principles of object-oriented programming:

  • Encapsulation – Hide implementation details in a class from users of the class, exposing only a public interface
  • Inheritance – Derive a subclass from a parent class, inheriting data and behavior from the parent, in an “is-a” relationship.  Inheritance defines a hierarchy of classes.  All classes ultimately inherit from System.Object.
  • Polymorphism – Any subtype may be used where a parent type (or type higher up in the class hierarchy) is expected.  Conversely, a variable of a particular class will be treated as the appropriate subclass.