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 (_model and _manifactureYear) can only be accessed / modified by functions defined inside the class (in this case getters and setters). Access outside the class body is done via helping members (model and manifactureYear).
  • If we uncomment line 50 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.