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 8
Classes Revisited
8.1 Class Containment ...................................................................... 159
8.2 Inheritance and the super Keyword .................................................... 160
8.3 Multiple Inheritance .................................................................... 163
8.4 Constructors of Subclasses .............................................................. 164
8.5 Abstract Classes and Methods ......................................................... 166
8.6 Auto-casting, Polymorphism, and Dynamic Binding ................................... 167
8.7 Interfaces and the Comparable Interface ............................................... 172
8.8 Access Privileges ........................................................................ 175
8.9 The final Keyword .................................................................... 178
8.10 Static Methods and Polymorphism ..................................................... 178
8.11 Explicit Type Checking ................................................................. 180
8.12 Cloning Objects ......................................................................... 181
8.13 Comparing Objects for Equality ........................................................ 184
8.14 Summary ................................................................................ 186
8.15 Syntax .................................................................................. 186
8.16 Important Points ....................................................................... 187
8.17 Exercises ................................................................................ 188
8.18 Lab ...................................................................................... 190
8.19 Project .................................................................................. 190
This chapter covers advanced topics related to classes. Inheritance allows one class to inherit
the methods and data of another class. This is often referred to as an is-a relationship.
For example, a manager is-a an employee. Therefore the Manager class may inherit from
the Employee class. Polymorphism allows dynamic method invocation. For example, we
can create several print methods in classes and subclasses. Then, we can call the print
method on an object. The appropriate print method will be executed based on the type of
the object. Abstract classes and interfaces are special classes that cannot be used to create
objects directly. Instead, they are templates from which other classes can inherit. While an
abstract class can contain regular methods and data, an interface contains only methods
with no bodies and constants.
8.1 Class Containment
Consider the DiceCup class from Chapter 6.
public c la ss DiceCup {
private Die [ ] d ic e ;
...
}
The DiceCup class contains an array of objects of type Die. Figure 8.1 shows an example
of a DiceCup object. This is referred to as class containment (or class composition). Every
object of type DiceCup can contain an array of die objects. In the figure, the object of type
159
160 Learning Java through Games
DiceCup object
value = 3
value = 5
value = 2
dice array
FIGURE 8.1: Object composition.
DiceCup contains three dice with values: 3, 5, and 2. Note that it is possible that memory
for the array has not been allocated, that is, a DiceCup object may contain no dice.
Class composition is sometimes referred to as the has relationship. For example, a
DiceCup has Die objects. Class composition is implemented by defining a variable of
one type inside a class of a different type.
An object that contains other objects is sometimes referred to as a composite object.As
we will see later in this chapter, special care needs to be taken when copying composite
objects. Not only the data inside the object, but also all the objects inside it need to be
copied.
8.2 Inheritance and the super Keyword
Let us consider a game of villains and superheroes. The player can create villains and
superheroes. For villains, the player needs to specify the name of the villain and the strength
of their evil power and narcissism. For superheroes, the player needs to specify how much
they are respected and the strength of their good powers. All strengths will be represented
as an integer between 1 and 10. The program will make the fictional characters fight each
other and report the results. A possible design of the two classes is shown next.
public c la ss Superhero {
private String name;
private int goodPower ;
private int respect ;
public void setName( St rin g name ) {
Classes Revisited 161
this .name = name;
}
public String getName () {
return name ;
}
...
}
public c la ss Villain {
private String name;
private int evilPower ;
private int narcissism ;
public void setName( St rin g name ) {
this .name = name;
}
public String getName () {
return name ;
}
...
}
Note that the two classes share attributes and methods. They both have the name
attribute and they both have the setName and getName methods. This is not a coincidence.
Both superheroes and villains are fictional characters and therefore they both inherit
attributes and methods from the FictionalCharacter class. A better design is shown
next.
public c la ss FictionalCharacter {
private String name;
public void setName( St rin g name ) {
this .name = name;
}
public String getName () {
return name ;
}
}
public c la ss Villain extends FictionalCharacter{
private int evilPower ;
private int narcissism ;
...
}
public c la ss Superhero extends FictionalCharacter {
private int goodPower ;
private int respect ;
...
}
Now the classes Villain and Superhero inherit from the FictionalCharacter class.
Our design is shown in Figure 8.2. Such figures are sometimes referred to as UML dia-
grams, where UML stands for Unified Modeling Language. In a UML diagram, every class is
surrounded by a rectangle. The rectangle contains the class name, variables, and methods.
Arrows between classes are used to represent inheritance.

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