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”

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

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

curso-poo-caja-fuerte

Object encapsulating itself

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

How to use encapsulation

The ENCAPSULATION is achieved through the introduction of 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… logically there would be no encapsulation 🤷‍♂️)

Visibility attributes define what is internal and what is public, within a OBJECT. Generally, 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
}
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

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

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

Example of Encapsulation in different languages

Let’s see what the syntax for defining a Car class would look like 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 precede each property or method.

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

In C++, members are declared as public or private 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
};

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

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

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

Where does encapsulation come from?

The role of ENCAPSULATION is very reasonable. Remember that before Object-Oriented Programming, there was already a trend to group variables within groupings in the form of structures.

pilares-oop

The four pillars of OOP

However, there was no limitation on any other part of the program coming and changing a data point. Which today, for most, is crazy.

Additionally, at that time, they were already making “pseudo-objects,” putting references to functions within these groupings. References to functions that could also change from anywhere in the program, without warning 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 “free-for-all.” That’s why the need for ENCAPSULATION arises.

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