Encapsulation Principle

Encapsulation is one of the 4 main OOP principles.

It protects the class data from access outside the class body.

Example of encapsulation



  • The private members can only be accessed / modified by functions defined inside the class (in this case getters and setters).
  • If we uncomment line 33 the code won't compile. A direct access to private members cannot be done outside the class body.

Example of no encapsulation


All members are accessible outside the class body.