Search code examples
c#oopencapsulation

Difference between Data hiding & Encapsulation in C#?


I've been going through some blog posts and StackOverflow answers to find how encapsulation is implemented in C#(Encapsulation in C# - .NET tutorials and Encapsulation in C# - JavaTPoint). All these blog posts and answers define encapsulation as expected.

Binding data and methods togeather in a type is called encapsulation

However, on the implementation part they say that it is implemented in C# by defining private variables and providing access to them using accessors and mutators/ properties. My question is how is this not data hiding? and why is it considered encapsulation? Moreover, shouldn't classes, structs(types) etc. be how encapsulation is implemented in C# since within these we bind data fields and functionalities togeather.


Solution

  • @Unmesh Kondolikar has a very clean and clear answer to this question here.

    Encapsulation is programming language feature which enables data hiding. However note that you can do data\information hiding even without encapsulation. For example using modules or functions in non Object Oriented programming languages. Thus encapsulation is not data hiding but only a means of achieving it.

    There has been some ambiguity between the usage of terms Information hiding and Encapsulation.(refer here.) In conclusion, Encapsulation refers to grouping of related entities(attributes and functionalities) into one unit which can be later referred to by single name. e.g. in procedural programming grouping of repeated code into procedures can be thought of as encapsulation. A designer can refer to such procedure with a common name. In OOP this concept is expanded from subroutines to classes and structures. Any class defining public instance variables is still encapsulated(although, this is poor design choice), but it doesn't implement data hiding.

    Encapsulated code that doesn't implement data hiding can have following problems when we try to make changes to them. As an example of information hiding/encapsulation, consider this class: (source)

    public class BankAccount {
        public int dollars;
    }
    

    The implementation of this class is encapsulated, but still inflexible (e.g. we cannot easily add support for individual cents in the future) and unsafe (e.g. the account can changed to be negative). However, if we hide the data behind a formally defined interface of methods, we gain flexibility and safety.

    public class BankAccount {
        private int dollars;
    
        public void deposit(int dollars) {
            this.dollars += Math.max(0, dollars);
        }
    }
    

    We now have control over how the state is modified, and we can also change the implementation without breaking client code:

    public class BankAccount {
        private int cents;
    
        public void deposit(int dollars) {
            deposit(dollars, 0);
        }
    
        public void deposit(int dollars, int cents) {
            this.cents += Math.max(0, 100 * dollars) + Math.max(0, cents);
        }
    }
    

    The class is now better encapsulated because we have hidden information about its underlying implementation.