Lesson Weekend

We can determine a field's visibility by changing its access modifier. When we make all fields private and manage data manipulation inside an object's own class, we are using encapsulation. Both are very important concepts in object-oriented programming across many different languages.

But you might be wondering why we need to use private methods. Why use getters and setters at all, especially since we had access to read and write fields when they were public?

An Example of Encapsulation


Imagine a User class with a private _password field. As developers, we wouldn't want to expose that field, making it public. Instead, we would want to provide a public CheckPassword() method that returns a bool that tells us if an entered password is correct.

Outside the class, I can only determine if a provided password is correct by using the CheckPassword() method. This means the sensitive information stored in the _password field is never exposed.

This is what getters and setters allow us to do: control how information is managed. A setter could perform a validation action before modifying a value, for example. We can also debug more easily when we have methods that perform data manipulation. If there's an error, the bug will be traced to that method.

Benefits of Encapsulation


Encapsulating classes is considered the best, most professional practice for several reasons:

  • It allows a class to have total control over its own fields, which is more secure.

  • It prevents other classes from accessing and altering fields, which can lead to difficult bugs.

  • While we may not see this benefit until our applications become larger, encapsulation leads to more maintainable code. Imagine we eventually need to change the data type of an object's public field. If outside classes were directly accessing this field (ie: testCar.price;, instead of testVehicle.GetPrice();), we would need to update all the code in outside classes that reference this field. This might not seem like a big deal, but imagine a C# application with tens or hundreds of classes. We would have to comb through each one and change all references to this field. However, if the field were private and outside classes accessed it through its getter method, only the object's getter method and class would need to be altered. We could update this class independently without affecting other classes that rely on it.

  • Managing all aspects of a class within the class itself leads to more organized code. Organized code allows other developers to comprehend, collaborate, and implement your logic much more easily.

From this point forward, all class fields should be private and getter and setter methods should be defined as needed.

An additional Resource

Check out this stack overflow post for another look at the many reasons why getters and setters are helpful.

Terminology


  • Encapsulation: The process of limiting access to fields so they are available only to a limited part of an application such as a class. It's a best practice because:

    • It allows a class to have total control over its own fields, which is more secure.
    • It prevents other classes from accessing and altering fields, which can lead to difficult bugs.
    • It leads to more maintainable and better organized code.
  • Visibility: The degree to which a field can be seen. If it's private, it can only be seen within the class. If it's public, it can be seen anywhere in the application.

Lesson 7 of 10
Last updated more than 3 months ago.