Are there more implementations of encapsulation?

Viewed 36

What I knew about encapsulation in OOP is that it refers to the restriction to the access of some data of an object. I thought that we mainly achieve encapsulation by the usage of access modifiers and properties inside of a class that allow us to validate the data before exposing it to the outer classes but in this article it's written that encapsulation also refers to bundling of data and functions into a single unit which is achieved by using/creating a class.

Honestly this is something I was not aware of until now and I am curious if there is a concrete definition of what encapsulation is and also I want to ask: Are there more examples and usages of encapsulation than using properies, classes and access modifiers?

Thank you in advance!

1 Answers

As wiki says about encapsulation:

encapsulation refers to the bundling of data with the methods that operate on that data, or the restricting of direct access to some of an object's components. Encapsulation is used to hide the values or state of a structured data object inside a class, preventing direct access to them by clients in a way that could expose hidden implementation details or violate state invariance maintained by the methods.

And an example of encapsulation can be:

public class Pizza
{
    private decimal cost;
    public decimal Cost() { return cost };
}

cost field is encapsulated. You have a method Cost() to get value of cost variable.

Related