ABSTRACT

A major problem with the original solution to the Yahtzee game was the use of global variables for the dice. Several methods that did not need access to these variables still had access. If the value of one of the die is wrong (e.g., equal to −3), then the bug may lie in any of the methods. The modified solution fixed this problem, but it was based on the premise that the dice were stored as an array of integers. Classes provide an even better solution. The dice can be encapsulated inside a class. This means that the code outside the class will not have direct access to the dice. Only the interface of the class (e.g., a set of methods) will be exposed to the outside world. As a result, the class can change how the dice are stored without affecting any code outside the class (e.g., a new implementation can store the dice as an array of short integers). This is referred to as data abstraction (i.e., the way the data is stored inside the class is abstracted). Data encapsulation and data abstraction are the pillars of the object-oriented paradigm.