Java Reference
In-Depth Information
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
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
Abstract Classes and Methods
.........................................................
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 class DiceCup
{
private Die [ ] dice ;
...
}
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
 
Search WWH ::




Custom Search