OOP. Encapsulation

      Comments Off on OOP. Encapsulation

This topic contains 0 replies, has 1 voice, and was last updated by  Васильев Владимир Сергеевич 3 weeks ago.

  • Author
    Posts
  • #4078

    To put it simple, encapsulation means placing all data type characteristics within the type itself, for further data and functionality access control.

    In other words, we provide everything related to the data type (fields, functions, properties, etc.) within the type itself, in order to protect and clearly distinguish its logic. Why do we need this?

    Any object is the owner of its characteristics, including behavior. Consider the class Person. It has a number of individual characteristics, such as name, eye color, blood group, age, etc. As well as behavioral characteristics, for example, any person can breathe, speak, hear, walk etc.

    Any particular instance of a Person class includes all the data and functionality provided by its class (individual set). But they might all have different values of those data fields.

    For example, let’s describe the Person class:

    class Person
    {
          public string Name { get; protected set; }
          public string EyesColor { get; protected set; }
          public string BloodGroup { get; protected set; }
          public int Age { get; protected set; }
         public Person(string name, string eyesCol, string bloodGr, int age) 
        { 
               Name = name;
               EyesColor = eyesCol;
               BloodGroup = bloodGr;
               Age = age;
         }
          public void Walk()
         {
            //walking algorithm
          }
          public void Talk()
          {
            //talking algorithm
          }
          public void Breathe()
          {
            //breathing algorithm
          }
         public void Speak()
          {
             //speaking algorithm
          }
     }

    When testing it, we create two instances of a Person class. Each of them owns a complete set of data defined within the class, as well as similar behavior.

    class Program
        {
            static void Main()
            {
                //creating 2 instances of a 
    class
                Person p1 = new Person("John",
     "Blue", "1A", 47);
                Person p2 = new Person("Ian", 
    "Gray", "2B", 32);
                //testing data
                Console.WriteLine("Name - {0},
     Age - {1}, Eyes color - {2}, BloodGroup - {3}", p1.Name, p1.Age, 
    p1.EyesColor, p1.BloodGroup);
                Console.WriteLine("Name - {0},
     Age - {1}, Eyes color - {2}, BloodGroup - {3}", p2.Name, p2.Age, 
    p2.EyesColor, p2.BloodGroup);
                //calling methods
                p1.Breathe();
                p2.Breathe();
                p1.Speak();
                p2.Speak();
                p1.Walk();
                p2.Walk();
            }
        }

    The second reason to provide encapsulation is that it’s the way not only to separate data of different instances of the same data type, but also to control access to them.

    Why do we need access control to data type members? Because we don’t want class data to be corrupted.

    For example, all the characteristics that we have set to the Person class objects when they were created are permanent. Other objects can read them, but they can not change them. This part is reasonable in our case cause after all, neither blood group nor eye color, nor the name can be changed after they had been set. These parameters are assigned to the objects when they are “born”, using the constructor. And further on we use different access modifiers for reading and writing data. As a result – we can read the values of the fields, but cannot change them.

    Now we can make a conclusion that a Person class ENCAPSULATES its data and functionality.

You must be logged in to reply to this topic.