1

I am very confused about encapsulation. My current concept of encapsulation is that it is only used for data hiding.

My code:

class Program
    {
        static void Main(string[] args)
        {
            Shape a;
            a= new Shape();
            a.Area = 4;
            Console.WriteLine(a.Area);
        }
    }

    class Shape
    {
        private int _area = 0;
        private int _parameter = 0;
        public int Area { get; set; }
        public int Parameter { get; set; }

    }

From what I understand, the private int variable _area is now encapsulated. So, what encapsulation is doing here is by using private access modifier, I can hide this variable; and by using public property, I can initialize this variable without using the class variable.

Is this what encapsulation is all about? Please correct me if I am wrong.

rafvasq
  • 1,512
  • 3
  • 18
  • 48
Abid Ali
  • 1,731
  • 8
  • 26
  • 62
  • See: http://stackoverflow.com/questions/385361/what-are-the-different-types-of-encapsulation (other than the top answer). – Corak Oct 10 '13 at 07:29
  • 2
    As a side note: your variabel `_area` will never be used. When using autoproperties (the `{get; set;}` syntax), the backingfiled is created automaticallly *behind the scene*. – Koen Oct 10 '13 at 07:32
  • possible duplicate of [Why use properties when no logic is involved?](http://stackoverflow.com/questions/11165326/why-use-properties-when-no-logic-is-involved) – H H Oct 10 '13 at 07:37
  • @Koen, what that backingfield is supposed to do? – Abid Ali Oct 10 '13 at 07:39
  • 1
    Write a `class CalenderDate` without encapsulation (`public int Y,M,D;`) and then try to guarantee, as the writer of the class, that the instances will always contain a valid date. – H H Oct 10 '13 at 07:45
  • Have a read about the [auto-implemented properties](http://msdn.microsoft.com/en-us/library/bb384054.aspx). If you still have a question regarding them, I suggest to open a new question for that. – Koen Oct 10 '13 at 08:20

1 Answers1

4

It's not just Information Hiding

Encapsulation means that a group of related properties, methods, and other members are treated as a single unit or object.

According to the principle of encapsulation, a class or struct can specify how accessible each of its members is to code outside of the class or struct. Methods and variables that are not intended to be used from outside of the class or assembly can be hidden to limit the potential for coding errors or malicious exploits.

Rajesh Subramanian
  • 6,400
  • 5
  • 29
  • 42
  • 2
    Helping against coding errors - Yes. But you won't stop any "malicious exploits" with encapsulation. – H H Oct 10 '13 at 07:35
  • @Rajesh, so here my code is not implementing the concept of ENCAPSULATION you mean to say? – Abid Ali Oct 10 '13 at 07:40
  • Now both Area and parameter is public, that means that there no protection or limitation for outside uses. If your system needs some protection like only Area can be exposed and parameter should be exposed only to same assembly u can make protected set for Parameter.. In this case, it is encapsulated – Rajesh Subramanian Oct 10 '13 at 07:43
  • @Henk Malcious exploits can be avoided if we use encapsulation sensibly, if you are having a class that has public property of Connection string, it can exploited for sql server connection details – Rajesh Subramanian Oct 10 '13 at 07:45
  • Encapsulation is absolutely not about security. That is a misconception. Many ways around it, Reflection being the easiest. – H H Oct 10 '13 at 07:47
  • @RajeshSubramanian what do you mean by this statement of yours: a GROUP of related properties, methods, and other members – Abid Ali Oct 10 '13 at 07:57
  • @AbidAli It means that a package or class that contains it own properties , methods and others related to it . – Rajesh Subramanian Oct 10 '13 at 08:48