Skip to content

Encapsulation (x-ers)

Estimated time to read: 2 minutes

Overview

The meaning of Encapsulation, is to make sure that "sensitive" data is hidden from users. To achieve this, you must:

  • declare class variables/attributes as private
  • provide public get and set methods to access and update the value of a private variable

Encapsulation forms a self-contained object by bundling the data and functions it requires to work, exposes an interface whereby other objects can access and use it, and restricts access to certain inside details.

encapsulation-in-java.png

x-ers

Getters and Setters allow modifying private variable values that are contained within an instance of a class.

By using Getters and Setters to modify and read these values, we add a layer of abstraction through the use of encapsulation.

Prohibiting direct access to these private variables means that additional code, usually for verification of a value, can be used within the getter and setter methods.

Getters

Setters

Attributes

Each object has a set of attributes associated with it. These attributes can be shared across a larger group of objects of the same type, and still be valid. For example:

Object (Abstraction) Attributes Methods (Behaviours / Responsibilities )
dog breed size hair length ear type color name sleeping eating playing

Context / Specific Perspective Attributes

Context or specific perspective is critical when forming an generalisation. The context that an object is within can drastically affect the properties of its abstraction.

For example, in an academic setting, a student will have the following attributes:

  • The courses they're currently taking
  • Their grades in each course
  • Their student ID number.

Whereas in a social setting, these attributes may not apply, so changing scope to fit within the specific perspective is required.

The actual values of these attributes may change, but the attributes themselves do not.