26.07.2013 Views

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

414 Object-Based <strong>Program</strong>ming Chapter 8<br />

8.11 Composition: Objects as Instance Variables of Other<br />

Classes<br />

An AlarmClock class object needs <strong>to</strong> know when it is supposed <strong>to</strong> sound its alarm, so<br />

why not include a reference <strong>to</strong> a Time object as a member of the AlarmClock object?<br />

Such a capability is called composition. A class can have references <strong>to</strong> objects of other<br />

classes as members.<br />

Software Engineering Observation 8.18<br />

One form of software reuse is composition, in which a class has references <strong>to</strong> objects of other<br />

classes as members. 8.18<br />

The next program contains three classes—Date (Fig. 8.13), Employee (Fig. 8.14) and<br />

EmployeeTest (Fig. 8.15). Class Employee contains instance variables firstName,<br />

lastName, birthDate and hireDate. Members birthDate and hireDate are references<br />

<strong>to</strong> Dates that contain instance variables month, day and year. This demonstrates<br />

that a class can contain references <strong>to</strong> objects of other classes. Class EmployeeTest instantiates<br />

an Employee and initializes and displays its instance variables. The Employee construc<strong>to</strong>r<br />

(Fig. 8.14, lines 12–20) takes eight arguments—first, last, birthMonth,<br />

birthDay, birthYear, hireMonth, hireDay and hireYear. Arguments birth-<br />

Month, birthDay and birthYear are passed <strong>to</strong> the Date construc<strong>to</strong>r (Fig. 8.13, lines<br />

13–28) <strong>to</strong> initialize the birthDate object and hireMonth, hireDay and hireYear<br />

are passed <strong>to</strong> the Date construc<strong>to</strong>r <strong>to</strong> initialize the hireDate object.<br />

A member object does not need <strong>to</strong> be initialized immediately with construc<strong>to</strong>r arguments.<br />

If an empty argument list is provided when a member object is created, the object’s<br />

default construc<strong>to</strong>r (or no-argument construc<strong>to</strong>r if one is available) will be called au<strong>to</strong>matically.<br />

Values, if any, established by the default construc<strong>to</strong>r (or no-argument construc<strong>to</strong>r)<br />

can then be replaced by set methods.<br />

Performance Tip 8.2<br />

Initialize member objects explicitly at construction time by passing appropriate arguments<br />

<strong>to</strong> the construc<strong>to</strong>rs of the member objects. This eliminates the overhead of initializing member<br />

objects twice—once when the member object’s default construc<strong>to</strong>r is called and again<br />

when set methods are used <strong>to</strong> provide initial values for the member object. 8.2<br />

Note that both class Date (Fig. 8.13) and class Employee (Fig. 8.14) are defined as<br />

part of the package com.deitel.jhtp4.ch08 as specified on line 3 of each file.<br />

Because they are in the same package (i.e., the same direc<strong>to</strong>ry), class Employee does not<br />

need <strong>to</strong> import class Date <strong>to</strong> use it. When the compiler searches for the file Date.class,<br />

the compiler knows <strong>to</strong> search the direc<strong>to</strong>ry where Employee.class is located. Classes<br />

in a package never need <strong>to</strong> import other classes from the same package.<br />

1 // Fig. 8.13: Date.java<br />

2 // Declaration of the Date class.<br />

3 package com.deitel.jhtp4.ch08;<br />

4<br />

5 public class Date extends Object {<br />

6 private int month; // 1-12<br />

Fig. Fig. 8.13 8.13 Date class (part 1 of 2).<br />

© Copyright 1992–2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!