que-es-encapsulacion-objetos-en-programacion

What is object encapsulation

  • 4 min

ENCAPSULATION is a fundamental principle that refers to hiding the internal details of an object and exposing them only through controlled access to its data and methods.

Encapsulation is an object saying: “Don’t touch my stuff”

OBJECTS have internal variables that contain their data and state. These variables are not available to everyone, just as you don’t like people messing with your kidney.

If another element wants to interact with an object, it must do so through its public methods. This way, the object can perform the necessary actions to maintain its internal consistency.

curso-poo-caja-fuerte

Object encapsulating itself

Object-oriented languages restrict internal data from being accessed from outside the object itself (it may seem obvious or logical, but other languages didn’t do it… and some still don’t 🙂).

How to Use Encapsulation

ENCAPSULATION is achieved by introducing visibility attributes to the methods and variables of a class.

Along with this, the programming language, compiler, and interpreter must support and respect it (because if they didn’t respect it, well… logically you wouldn’t have encapsulation 🤷‍♂️)

Visibility attributes define what is internal and what is public within a OBJECT. In general, there should be at least two types of visibility.

  • Private, accessible only by the Object itself
  • Public, accessible by other elements
class Person
{
	public name = "Luis"     // public attribute
    private age = 12        // private attribute
}
Copied!
person.name = "another name" ✔️   // it's public, you can read and write it
person.age = 14// it's private, you CANNOT read or write it
Copied!

Although other languages may add more forms of visibility, such as protected or internal, for example. Or differentiate between write visibility and read visibility.

But they must at least have private and public to talk about encapsulation.

Example of Encapsulation

Let’s see what the syntax would be for defining a Car class in different programming languages.

In C#, the keyword public is used to declare an attribute as public and private to declare it as private. These attributes are placed before each property or method.

public class Person
{
    public string name = "Luis";  // public attribute
    private int age = 12;         // private attribute
}
Copied!

In C++, declaring members as public or private is done using the access modifiers public and private, which define “sections” within a class.

class Person {
public:
	string name = "Luis";  // public attribute
	
private:
    int age = 12;           // private attribute
};
Copied!

In JavaScript, there is no explicit distinction between public and private attributes. However, by using naming conventions (using # before the variable name), you can indicate that an attribute is private.

class Person {
	name = 'Luis';   // public attribute
	#age = 12;       // private attribute
}
Copied!

Python does not have access modifiers, but a convention is used to indicate that an attribute is private (prefix with __ double underscore).

class Person:
    def __init__(self):
        self.name = 'Luis'  # public attribute
        self.__age = 12     # private attribute
Copied!

Where Does Encapsulation Come From?

The role that ENCAPSULATION fulfills is very reasonable. Remember that, before Object-Oriented Programming, there was already a trend of grouping variables within structures.

pilares-oop

The four pillars of OOP

However, there was no limitation preventing any other part of the program from coming and changing your data. Which, today, for most people is crazy.

Furthermore, at that time they were already making “pseudo-objects,” putting function references inside these groupings. References to functions that could also be changed from any part of the program, without warning or restriction.

So in these kinds of “proto-objects,” anyone could come and change the internal state, or even the behavior. And you can imagine, that was “chaos.” That’s why the need for ENCAPSULATION arises.

Since the concept of OBJECT as an elementary unit of work was being created, it was logical that they needed to be “armored” so that each one was isolated and its access protected.