Encapsulation is implemented by using access specifiers. An access specifier defines the scope and visibility of a class member.
- Public access specifier allows a class to expose its member variables and member functions to other functions and objects. Any public member can be accessed from outside the class.
- Private access specifier allows a class to hide its member variables and member functions from other functions and objects. Only functions of the same class can access its private members. Even an instance of a class cannot access its private members.
- Protected access specifier allows a child class to access the member variables and member functions of its base class. This way it helps in implementing inheritance.