O'Reilly logo

Learning Java Through Games by Lubomir Stanchev

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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.
Thedicecanbeencapsulated 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
110 Learning Java through Games
is instance of the Person class. Do not forget that object variables always start with a
lowercase letter, while class names always start with a capital letter.
Instance variables are associated with the objects of a class. Every instance of a
class can have different values for the instance variables.
For example, we can create a new person as follows.
Person p2 = new person (" Bob" );
Now p1 and p2 are two objects of the Person class that have a different value for the
instance variable name. An instance variable of a class is a variable that is associated with
every object of the class. For example, if there are 100 objects of type Person, then 100
variables name will be created, one for every object.
6.2 Class Interaction and Data Encapsulation
Let us revisit the Yahtzee game from the previous chapter, but this time build a better
design that utilizes the object-oriented paradigm. When creating an object-oriented solu-
tion, we try to build classes and objects that model real-world entities. For example, if
we are building bank software, then we may identify the classes: BankAccount, Customer,
Teller, and so on. The customer Peter is an example of an object that belongs to the
Customer class.
If we go back to the Yahtzee game, a die is an example of a real-world entity that we
would like to model. We will create a Die class, where a particular die will be an object (or
instance) of the class. The pillar of every class is its data. The data inside a class represents
the variables of each object in the class. In our case, every die has a dieValue that is an
integer. Next, the methods of the class need to be identified. Usually, only methods that
access or manipulate the data of the class belong to the class. In our case, this can be a
method that sets and a method that reads the value of the die. A first iteration of the Die
class follows.
class Die {
private int dieValue ;
public void setValue( int value ){
dieValue = value ;
}
public int getValue () {
return dieValue ;
}
}
The variable dieValue is defined as a private variable. This means that it is only
accessible inside the class in which it is defined. Nobody outside the Die class knows about
its existence. The setValue and getValue methods are defined as public. This means that
they can be accessed from everywhere in our program.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required