Free Ebook cover Complete Logic Programming Course for Beginners

Complete Logic Programming Course for Beginners

4

(3)

83 pages

Encapsulation: Object Oriented Programming

Capítulo 76

Estimated reading time: 3 minutes

+ Exercise
Audio Icon

Listen in audio

0:00 / 0:00

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.

Continue in our app.

You can listen to the audiobook with the screen off, receive a free certificate for this course, and also have access to 5,000 other free online courses.

Or continue reading below...
Download App

Download the app

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.

Encapsulation in Object Oriented Programming is a technique that restricts direct access to an object's elements, hiding the internal state to ensure data integrity and security. This is achieved using access modifiers and accessor methods, like getters and setters, to control how attributes are accessed and modified.

Next chapter

Data Structures (Lists, Stacks, Queues)

Arrow Right Icon
Download the app to earn free Certification and listen to the courses in the background, even with the screen off.