19.7. Encapsulation: Objects

Página 68

19.7 Encapsulation: Objects

Encapsulation is one of the four main principles of the object-oriented programming (OOP) paradigm, along with inheritance, polymorphy, and abstraction. It is a technique that involves restricting direct access to some components of an object and providing methods (functions) to manipulate the values ​​of those components.

Encapsulation is often referred to as the act of packaging the data (variables) and the methods (functions) that operate on that data into a single unit, which is the object. In other words, encapsulation is the idea of ​​hiding an object's internal state and requiring that interaction with that object be performed through its methods.

Why is Encapsulation important?

Encapsulation is important for several reasons. First, it helps maintain the integrity of the data within an object by ensuring that it cannot be changed inappropriately or unexpectedly. This is done by hiding an object's attributes and providing public methods to access and modify those attributes.

Second, encapsulation contributes to code modularity. Because an object's implementation details are hidden, other objects don't need to understand or care about the way that object works internally. This makes the code more manageable, more scalable, and less prone to errors.

Finally, encapsulation facilitates code reuse. Because an object encapsulates its data and methods, it can be easily reused in different parts of a program or across different programs.

How does Encapsulation work?

Encapsulation is implemented a little differently in different programming languages, but the basic idea is the same. In an object-oriented programming language like Java or C#, you define a class to create an object. The class includes variables for storing data (called fields or attributes) and functions for operating on that data (called methods).

By default, all fields and methods of a class are accessible to any code that can access the object. However, you can restrict access to some fields and methods using access modifiers. In Java and C#, for example, you can use the 'private', 'protected' and 'public' modifiers to control access to a class's fields and methods.

A 'private' field or method can only be accessed within the same class. A 'protected' field or method can be accessed within the same class and by subclasses. A 'public' field or method can be accessed by any code that can access the object.

To provide controlled access to private fields, you can use 'get' and 'set' methods. A 'get' method returns the value of a private field, and a 'set' method allows you to change the value of a private field. This allows you to control exactly how and when private fields can be accessed and modified.

Encapsulation Example

To illustrate how encapsulation works, let's consider a simple example. Suppose we have a 'Person' class with two private fields: 'name' and 'age'.

class Person {
    private String name;
    private integer;
}

Since 'name' and 'age' are private, they cannot be accessed directly from outside the 'Person' class. To allow controlled access to these fields, we can add 'get' and 'set' methods for each of them:

class Person {
    private String name;
    private integer;

    public String getName() {
        return name;
    }

    public void setName(String newName) {
        name = newName;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int newAge) {
        if (newAge >= 0) {
            age = newAge;
        }
    }
}

Here, 'getName' and 'getAge' are 'get' methods that return the values ​​of 'name' and 'age', respectively. 'setName' and 'setAge' are 'set' methods that allow you to change the values ​​of 'name' and 'age' respectively. Note that 'setAge' checks whether the new age is greater than or equal to zero before changing 'age'. This is an example of how encapsulation can be used to maintain data integrity.

Conclusion

In summary, encapsulation is a fundamental principle of object-oriented programming that helps maintain data integrity, promote code modularity, and facilitate code reuse. It does this by wrapping the data and the methods that operate on that data in objects and restricting direct access to some components of those objects.

Now answer the exercise about the content:

Which of the following is a fundamental principle of object-oriented programming that helps maintain data integrity, promote code modularity, and facilitate code reuse?

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

You missed! Try again.

Next page of the Free Ebook:

6919.8. Encapsulation: Encapsulation

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or 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