Encapsulation in Object-Oriented Programming

Welcome to this post where we will explore another pillar of object-oriented programming: encapsulation and abstraction.

 

 

1 - What is encapsulation in object-oriented programming

Encapsulation in object-oriented programming refers to limiting or restricting access to a property to only the elements that need it and no one else.

The most common example of encapsulation is classes, where we encapsulate and group both methods and properties.

Another very common example of encapsulation is getters and setters for properties within a class. By default, they give us the "normal" value, but we can modify them to change it.

private decimal _velocidadActual { get; set; }
public decimal VelocidadActual 
{
    get{
        return _velocidadActual + 2;
    }
    set{
        _velocidadActual = value;
    }
}

In the example we just saw, there are two properties, both referring to the current speed, but with slight differences.

One is private, so from outside the class, its value cannot be accessed.

The second is public and accesses the previously mentioned private one. Why do we do this?

The example above is a real-world case: cars usually display a few extra kilometers per hour compared to the actual speed. So we encapsulate that logic inside the setter, which is hidden from the consumer who, on their speedometer, sees the speed including the extra two kilometers per hour.

We can say that encapsulation is a way of hiding information between entities, showing only the most necessary information among them.

 

1.1 - Access Modifiers

As we have seen, encapsulation is tied to access modifiers, which are explained in a dedicated post about access modifiers.(link)

It’s still worth recalling them here.

  • Public – full access
  • Private – access only from within the class or struct that contains them
  • Internal – accessible from within the same project
  • Protected – accessible from within the same class or from derived classes
  • Protected internal – combines protected and internal, allowing access from the same project or from derived classes
  • Private protected – allows access from the current class or from classes derived from it

Note: Interfaces are also important and will be covered in the next post.

 

 

2 - Abstraction in object-oriented programming

Note: do not confuse with abstraction in C# (future post).

Abstraction is a concept very similar to encapsulation, with the main difference that abstraction allows us to represent the real world in a simpler way. We could also define it as a way of identifying necessary functionalities without going into the details of what we’re doing.

The clearest example is when we brake a car: for us, the user, it is simply pressing the brake, but behind the scenes, the car performs many actions. Imagine, too, that in every car, drivers brake in the same way, regardless of whether the brakes are disc or drum. The car, however, performs different actions.

Pressing the brake would be the level of abstraction.

 

 

3 - Differences between encapsulation and abstraction 

AbstractionEncapsulation
Seeks solutions in designSeeks solutions in implementation
Only relevant informationCode hiding for protection
Focused on executionFocused on execution

 

This post was translated from Spanish. You can see the original one here.
If there is any problem you can add a comment bellow or contact me in the website's contact form

Uso del bloqueador de anuncios adblock

Hola!

Primero de todo bienvenido a la web de NetMentor donde podrás aprender programación en C# y .NET desde un nivel de principiante hasta más avanzado.


Yo entiendo que utilices un bloqueador de anuncios como AdBlock, Ublock o el propio navegador Brave. Pero te tengo que pedir por favor que desactives el bloqueador para esta web.


Intento personalmente no poner mucha publicidad, la justa para pagar el servidor y por supuesto que no sea intrusiva; Si pese a ello piensas que es intrusiva siempre me puedes escribir por privado o por Twitter a @NetMentorTW.


Si ya lo has desactivado, por favor recarga la página.


Un saludo y muchas gracias por tu colaboración

© copyright 2025 NetMentor | Todos los derechos reservados | RSS Feed

Buy me a coffee Invitame a un café