Java Reference
In-Depth Information
Chapter 6
Introduction to Classes
6.1 Classes and Objects ..................................................................... 109
6.2 Class Interaction and Data Encapsulation ............................................. 110
6.3 Default Constructor .................................................................... 114
6.4 The toString Method .................................................................. 116
6.5 Instance versus Static ................................................................... 117
6.6 Non-empty Constructors and the Hidden Parameter this ............................ 118
6.7 Array of Objects and Multi-Class Solutions ............................................ 120
6.8 Multi-Class Solution to the Battleship Game .......................................... 123
6.9 Summary ................................................................................ 133
6.10 Syntax .................................................................................. 133
6.11 Important Points ....................................................................... 134
6.12 Exercises ................................................................................ 135
6.13 Lab ...................................................................................... 136
6.14 Project
.................................................................................. 136
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.
Thedicecanbe 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.
6.1 Classes and Objects
Consider the following definition.
int i;
The variable i is declared of type integer. The int keyword refers to the type, while i
is the variable name. Similarly, an object can be declared as follows.
Person p1 = new Person( "John" );
The declaration creates a new person with name John. Person is the name of the class
and p1 is the name of the object. A class contains variables and methods that manipulate
these variables. For example, the Person class may contain the variable name and the
getName and setName methods. Objects are instances of classes. For example, the p1 object
109
 
Search WWH ::




Custom Search