414 Object-Based Programming Chapter (Best web site) 8 8.11 Composition: Objects
414 Object-Based Programming Chapter 8 8.11 Composition: Objects as Instance Variables of OtherClasses An AlarmClock class object needs to know when it is supposed to sound its alarm, so why not include a reference to a Time object as a member of the AlarmClock object? Such a capability is called composition. A class can have references to objects of other classes as members. Software Engineering Observation 8.18 One form of software reuse is composition, in which a class has references to objects of other classes as members. The next program contains three classes Date(Fig. 8.13), Employee(Fig. 8.14) and EmployeeTest (Fig. 8.15). Class Employee contains instance variables firstName, lastName, birthDateand hireDate. Members birthDateand hireDateare references to Dates that contain instance variables month, dayand year. This demonstrates that a class can contain references to objects of other classes. Class EmployeeTestinstantiates an Employeeand initializes and displays its instance variables. The Employeeconstructor (Fig. 8.14, lines 12 20) takes eight arguments first, last, birthMonth, birthDay, birthYear, hireMonth, hireDayand hireYear. Arguments birth- Month, birthDayand birthYearare passed to the Dateconstructor (Fig. 8.13, lines 13 28) to initialize the birthDate object and hireMonth, hireDay and hireYear are passed to the Dateconstructor to initialize the hireDateobject. A member object does not need to be initialized immediately with constructor arguments. If an empty argument list is provided when a member object is created, the object s default constructor (or no-argument constructor if one is available) will be called automatically. Values, if any, established by the default constructor (or no-argument constructor) can then be replaced by set methods. Performance Tip 8.2 Initialize member objects explicitly at construction time by passing appropriate arguments to the constructors of the member objects. This eliminates the overhead of initializing member objects twice once when the member object s default constructor is called and again when set methods are used to provide initial values for the member object. Note that both class Date(Fig. 8.13) and class Employee(Fig. 8.14) are defined as part of the package com.deitel.jhtp4.ch08 as specified on line 3 of each file. Because they are in the same package (i.e., the same directory), class Employeedoes not need to import class Dateto use it. When the compiler searches for the file Date.class, the compiler knows to search the directory where Employee.class is located. Classes in a package never need to import other classes from the same package. 1 // Fig. 8.13: Date.java 2 // Declaration of the Date class. 3 package com.deitel.jhtp4.ch08; 5 public class Date extends Object { 6 private int month; // 1-12 Fig. 8.13 Dateclass (part 1 of 2). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01
Note: In case you are looking for affordable webhost to host and run your servlet application check Vision mysql5 web hosting services