Encapsulation is one of the four fundamental pillars of Object Oriented Programming (OOP). The other three are inheritance, polymorphism, and abstraction. Encapsulation is a mechanism that restricts direct access to an object's elements. It is used to hide the values ​​or state of a structured object, which are usually stored in instance variables.

Why is encapsulation important?

Encapsulation is critical to data integrity and security. It protects the object's data from unwanted modifications and ensures that only methods within the object can access and modify its attributes. This is vital to preventing data corruption and ensuring software reliability.

How does encapsulation work?

In practice, encapsulation is implemented using accessor methods - getters and setters - and accessor modifiers such as private, public and protected.

  • Getters: These are methods that allow access to object attributes. They are used to get the value of an instance variable.
  • Setters: These are methods that allow you to modify the object's attributes. They are used to set the value of an instance variable.
  • Access modifiers: These are keywords that define the level of access to an object's attributes and methods. Private means that the attribute or method can only be accessed within the class itself. Public means it can be accessed from anywhere. Protected means it can be accessed within the class itself and by subclasses.

Example encapsulation

Suppose we have a class called Person, which has two attributes: name and age. We want the name to be accessible and modifiable by anyone, but we want the age to be accessible and modifiable only by methods within the class itself. Here's how we could do that:

class Person {
  public String name;
  private int age;

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public int getAge() {
    return age;
  }

  private void setAge(int age) {
    if (age >= 0) {
      this.idade = age;
    }
  }
}

In this example, anyone can access and modify the name, but we can only access the age through the getAge() method and we can only modify the age through the setAge() method. Also, the setAge() method checks that the age is a valid (non-negative) value before modifying it.

Conclusion

Encapsulation is a powerful tool for preserving data integrity and for creating secure, reliable, and maintainable code. It lets you control who can access and modify an object's attributes, and how they can do so. This is vital to prevent data corruption and to ensure software reliability.

While it may seem a bit complicated at first, with practice and experience, encapsulation will become a natural part of your programming toolbox. And once you master encapsulation, you'll be well on your way to becoming a master of Object Oriented Programming.

Now answer the exercise about the content:

Which of the following is an accurate description of encapsulation in Object Oriented Programming?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Data Structures (Lists, Stacks, Queues)

Next page of the Free Ebook:

77Data Structures (Lists, Stacks, Queues)

3 minutes

Obtenez votre certificat pour ce cours gratuitement ! en téléchargeant lapplication Cursa et en lisant lebook qui sy trouve. Disponible sur Google Play ou App Store !

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text