Language: EN

que-es-encapsulacion-objetos-en-programacion

What is object encapsulation

The 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”

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

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

Object-oriented languages restrict access to internal data from outside the object itself. It may seem obvious or logical, but other languages didn’t (and still don’t) do it.

How to use encapsulation

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

In addition to 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. 🤷‍♂️

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

  • Private, only accessible by the Object itself
  • Public, accessible by other elements
class Persona
{
	public nombre = "Luis"     // public attribute
    private edad = 12          // private attribute
}
persona.nombre = "another name" ✔️   // it's public, you can read and write it
persona.edad = 14// it's private, you can't read or write it

Although other languages may add more visibility forms, such as protected or internal, for example. Or differentiate in visibility for writing, and visibility for reading.

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

Example of encapsulation in different languages

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

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

public class Persona
{
    public string nombre = "Luis";  // public attribute
    private int edad = 12;          // private attribute
}

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

class Persona {
public:
	string nombre = "Luis";  // public attribute
	
private:
    int edad = 12;           // private attribute
};

In JavaScript, there is no explicit distinction between public and private attributes. However, by using the naming convention (using # in front of the variable name), it can be indicated that an attribute is private.

class Persona {
	nombre = 'Luis';   // public attribute
	#edad = 12;        // private attribute
}

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

class Persona:
    def __init__(self):
        self.name = 'Luis'  // public attribute
        self.__edad = 12    // private attribute

Where does encapsulation come from?

The role played by ENCAPSULATION is very reasonable. Remember that, before Object-Oriented Programming, there was already a tendency to group variables within structures.

pilares-oop

The four pillars of OOP

However, there was no limitation to prevent any other part of the program from coming and changing a data. Which, nowadays, for most people is insane.

In addition, at that time they were already creating “pseudo-objects”, putting references to functions inside these groupings. References to functions that could also be changed from anywhere in the program, without notice or restriction.

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

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