Let’s understand what is Encapsulation in OOP
What is Encapsulation?
Encapsulation can be stated as “hiding entities which are not required for the context”.
In other words, it means to deny access to those features or contents which aren’t needed for the other components to get hold of.
The client or the requesting component need not know all the details of the component it is requesting, it just needs to be informed of only things it requires to get things done.
Encapsulation also means to wrap all the information and functionality which represent a responsibility together under a logical structure.
What does Encapsulation do?
Any structure which complies to Encapsulation does the following -
- Wraps up all things which share a common responsibility.
- Hides these things from any unwanted access
Encapsulating into Classes and Objects
The logical structure which provides the housing for all these entities is called a Class.
Any component which needs to access these entities wrapped within a Class by means of a physical reference is called an Object.
A Class provides a logical structure and a lock, while the object for a class is the key to its access.
An Object is a physical copy of the Class which is a logical structure.
Whenever we use a “new” keyword followed by the class type, we’re literally creating a “physical copy” of this otherwise logical entity in the memory and accessing the functionality and data out of it.
access modifiers and scopes
Access Modifiers specify the accessibility of a particular field or method inside a class from an outside world perspective.
The scope of accessibility can be — local, referential or inherited
- public — accessible from outside of a class, package or a subclass (local, referential and inherited)
- private — accessible from only within the class where the field or method is specified and nowhere else (local only).
- protected — accessible from within the class and within the derivative sub-classes only (local, inherited only).
- default / no modifier — the default access specifier provides access from within class only (local only)
Full article — https://referbruv.com/blog/object-oriented-programming-concepts-encapsulation/