Classes

Encapsulation is an important concept in object-oriented programming. In Java, we use the keywords "private" and "public" to enforce encapsulation. We also use setter and getter methods to enforce encapsulation -- i.e., we use setter and getter methods to control how people access and modify instance variables.

Below are two quotes describing the principle of encapsulation:

"Encapsulation...refers to the practice of including within an object everything it needs, and furthermore doing this in such a way that no other object need ever be aware of this internal structure."
-I. Graham, Object-Oriented Methods, Addison-Wesley, 1991

"Encapsulation...consists of separating the external aspects of an object which are accessible to other objects, from the internal implementation details of the object which are hidden from other objects."
-Rubaugh et al, Object-Oriented Modeling and Design, Prentice-Hall, 1991

Below are the Die class and a RollingDice class that creates 2 dice and calls various methods on them: