Encapsulation: Variables

Capítulo 62

Estimated reading time: 3 minutes

+ Exercise
Audio Icon

Listen in audio

0:00 / 0:00

19.1 Encapsulation: Variables

Encapsulation is one of the four fundamental pillars of object-oriented programming (OOP). It is a mechanism that restricts direct access to some components of an object and protects the members of an object from being accessed directly. In programming terms, encapsulation is the process of hiding the implementation details of a class and allowing access to it only through public methods.

Variables play a crucial role in encapsulation. To understand the importance of variables in encapsulation, we first need to understand what variables are. In programming, a variable is a named storage location that can hold some data or information that can change during program execution. Variables are the backbone of any program as they allow programmers to store, access, and manipulate data.

In the context of encapsulation, variables are often declared private to restrict direct access to them. This means that these variables cannot be directly accessed or modified from outside the class. Instead, they can only be accessed or modified through public methods known as getters and setters.

The getters and setters methods are used to set or get the value of private variables. The getter method returns the value of the private variable, while the setter method sets or modifies the value. These methods give you greater control over how variables are accessed and modified, helping to maintain data integrity.

For example, consider a class 'Person' with a private variable 'age'. To access or modify a person's age, we need to use getter and setter methods. The getter method 'getAge' returns the person's age, while the setter method 'setAge' modifies the age. If we try to access the 'age' variable directly from outside the class, we will get an error, as the variable is private and cannot be accessed directly.

Continue in our app.
  • Listen to the audio with the screen off.
  • Earn a certificate upon completion.
  • Over 5000 courses for you to explore!
Or continue reading below...
Download App

Download the app

class Person {
  private int age;

  public int getAge() {
    return age;
  }

  public void setAge(int age) {
    if (age > 0) {
      this.idade = age;
    } else {
      System.out.println("Invalid age!");
    }
  }
}

As you can see, the setAge method also includes a check to ensure that the age is valid (ie greater than zero). This helps ensure data integrity by preventing age from being set to an invalid value. This is an example of how encapsulation can be used to protect data and ensure that it is handled properly.

In summary, encapsulation is an important concept in object-oriented programming that helps protect data and ensure its integrity. Variables play a crucial role in encapsulation as they allow programmers to restrict direct access to data and ensure that it is handled appropriately. By using private variables and getter and setter methods, programmers can control how data is accessed and modified, providing greater control over the implementation of a class.

Now answer the exercise about the content:

What is encapsulation in object-oriented programming and what is the role of variables in this concept?

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

You missed! Try again.

Encapsulation is a key concept in object-oriented programming that involves hiding the implementation details of a class and exposing access through public methods. Variables are crucial in this process as they are usually declared private, preventing direct access from outside the class. Public methods, such as getters and setters, are used to manipulate these private variables, ensuring proper handling and data integrity.

Next chapter

Encapsulation: Data Types

Arrow Right Icon
Free Ebook cover Complete Logic Programming Course for Beginners
75%

Complete Logic Programming Course for Beginners

4

(3)

83 pages

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