Composition: Dr. Zara Hamid
Composition: Dr. Zara Hamid
Zara Hami d
COMPOSITION
An obj ect can contai n another obj ect. The rel ationship
between the two i s cal l ed composition.
Sometimes referred to as a has-a relationship
EXAMPLE
1 // Fig. 8.7: Date.java
2 // Date class declaration.
3
4 public class Date
5 {
6 private int month; // 1-12
7 private int day; // 1-31 based on month
8 private int year; // any year
9
10 // constructor: call checkMonth to confirm proper value for month;
11 // call checkDay to confirm proper value for day
12 public Date( int theMonth, int theDay, int theYear )
13 {
14 month = checkMonth( theMonth ); // validate month
15 year = theYear; // could validate year
16 day = checkDay( theDay ); // validate day
17
18 System.out.printf(
19 "Date object constructor for date %s\n", this );
20 } // end Date constructor
21
22 // utility method to confirm proper month value
23 private int checkMonth( int testMonth )
24 {
25 if ( testMonth > 0 && testMonth <= 12 ) // validate month
26 return testMonth;
27 else // month is invalid
28 {
29 System.out.printf(
30 "Invalid month (%d) set to 1.", testMonth );
31 return 1; // maintain object in consistent state
32 } // end else
33 } // end method checkMonth
34
35 // utility method to confirm proper day value based on month and year
36 private int checkDay( int testDay )
37 {
38 int daysPerMonth[] =
39 { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
40
41 // check if day in range for month
42 if ( testDay > 0 && testDay <= daysPerMonth[ month ] )
43 return testDay;
44
45 // check for leap year
46 if ( month == 2 && testDay == 29 && ( year % 400 == 0 ||
47 ( year % 4 == 0 && year % 100 != 0 ) ) )
48 return testDay;
49
50 System.out.printf( "Invalid day (%d) set to 1.", testDay );
51 return 1; // maintain object in consistent state
52 } // end method checkDay
53
54
59 } // end class Date
1 // Fig. 8.8: Employee.java
2 // Employee class with references to other objects.
3
4 public class Employee
5 {
6 private String firstName;
7 private String lastName;
8 private Date birthDate;
9 private Date hireDate;
10
11 // constructor to initialize name, birth date and hire date
12 public Employee( String first, String last, Date dateOfBirth,
13 Date dateOfHire )
14 {
15 firstName = first;
16 lastName = last;
17 birthDate = dateOfBirth;
18 hireDate = dateOfHire;
19 } // end Employee constructor
20
27 } // end class Employee
1 // Fig. 8.9: EmployeeTest.java
2 // Composition demonstration.
3
4 public class EmployeeTest
5 {
6 public static void main( String args[] )
7 {
8 Date birth = new Date( 7, 24, 1949 );
9 Date hire = new Date( 3, 12, 1988 );
10 Employee employee = new Employee( "Bob", "Blue", birth, hire );
11 System.out.println(employee.getFirstname()+employee.getLastname()+Hired+
employee.getHired()+Birthday+ employee.getBirthday());
13 } // end main
14 } // end class EmployeeTest
Date object constructor for date 7/24/1949
Date object constructor for date 3/12/1988
Blue, Bob Hired: 3/12/1988 Birthday: 7/24/1949
A fundamental desi gn choi ce that we have to make when consi deri ng
whether or not to use i nheri tance i s to exami ne the rel ati onshi p that
exi sts between two cl asses. The si mpl e way to do thi s i s the I s- a and
has- a rul e.
We say an obj ect X i s- a Y, i f ever ywhere you can use an obj ect of type Y,
you can use i nstead of obj ect of type X. I n other words, X i s a proper
subtype of Y. Usual l y, thi s means that X i mpl ements the same i nter face
as Y. So, you know that X and Y conform to the same set of method
type si gnatures, however, thei r i mpl ementati on may be di f ferent.
We say an obj ect X has- a Y, i f Y i s a par t - of X. So, you typi cal l y thi nk of
X contai ni ng an i nstance of Y, not X i nheri ti ng from Y.
For exampl e:
1. Woul d you say that an Emai l Message i s- a Header or an Emai l
Message has- a Header?
2. Woul d you say that Ci rcl e i s- a Shape or a Ci rcl e has- a Shape?
I n each case, you need to consi der whether or not the rel ati onshi p
between two obj ects i s si mpl e one of usi ng the obj ect or bei ng the
obj ect. I f you j ust need to use an obj ect, then that i mpl i es a
composi ti on rel ati onshi p. I f an obj ect behaves l i ke another obj ect, then
that i mpl i es a subtype rel ati onshi p
INHERITANCE VS COMPOSITION
Decl are a cl ass ti me. Al so i ncl ude a method di splayTime().
Decl are anotherclass StopWatch, whi ch has two obj ects of cl ass
ti me, startTime and stopTime.
Incl ude appropriate constructors
Incl ude a method di splay , whi ch pri nts the start ti me and stop
ti me
CLASS ACTIVITY
ATM si mulator
Phone si mulator
Games: TETRIS
Snake and ladder
CHESS
CARD GAME
Li brary Management System
Hotel Management System
http://mindprod.com/project/projects.html
FINAL PROJECT EXAMPLES