Encapsulation

Encapsulation is an important aspect of OO programming. It means that the inner workings of our classes/objects are not visible or accessible to the rest of the program. In other words each object is like a black box and only what it displays on the outside is accessible. All classes/objects in Java are encapsulated.

This has two advantages:

  1. The code within an encapsulated block of code can be modified and changed. Provided the attributes and methods are the same (The API) then this will have no effect on the rest of the program.
  2. It is not possible to reach for a program to 'reach in' and change a variable or attribute of an object without going through the API. This makes the code more stable and less prone to errors.
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License