Using Class Composition to Create a Class From Another

 

Now we are going to develop one class to deal with dates and then use this as part of another class to deal with basic employee information.  This is not really an extension, we are using instances of the data class within our development of the employee class.  This is called class composition.

Now the code for Date.java should be copied into a new directory.

// Date.java
// Declaration and development of the Date class

package edu.quinnipiac.javaProgs.time;

public class Date extends Object
{
private int month; // 1 - 12
private int day; // 1 - 31 based on month
private int year; // any year

// Constructor:
// Confirm proper value for month
// Call method checkDay to confirm proper value for day
public Date (int mm, int dd, int yy)
{
if (mm > 0 && mm <= 12)
month = mm;
else
{
month = 1;
System.out.println("Month" + mm + "is invalid. Set month to 1.");
}
year = yy;
day = checkDay(dd);
System.out.println("Date object construstor for date" + toString());

}

// Utility method to confirm proper day value based on month and year
public int checkDay( int testDay)
{
int daysPerMonth[ ] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

if (testDay > 0 && testDay <=daysPerMonth[month])
return testDay;

// Checking for leap years so that February has 29 days

if (month == 2 && testDay == 29 && (year%400 == 0 || (year % 4 == 0 && year % 100 != 0 )))
return testDay;

System.out.println("Day" + testDay + "is invalid. Set day to 1.");
return 1;
}

// Create a string of the form mm/dd/yy
public String toString()
{
return month + "/" + day + "/" + year;
}
}

In order to compile this class and locate it appropriately you need to give the command

javac -d c:\jdk1.2.1\jre\classes Date.java

This will cause the Date.class to be located along the path 

C:\jdk1.2.1\jre\classes\edu\quinnipiac\javaProgs\time

which can be seen in the following image.

 

 

Now you want to copy the following code into a file called Employee.java in the same directory.  I needed to include an import statement that the book didn't have in order to get it to compile.

// Employee.java
// A class for basic employee information

package edu.quinnipiac.javaProgs.time;
import edu.quinnipiac.javaProgs.time.Date;

public class Employee extends Object
{
private String firstName;
private String lastName;
private Date birthDate;
private Date hireDate;

public Employee(String fName, String lName,
int bMonth, int bDay, int bYear,
int hMonth, int hDay, int hYear)

{
firstName = fName;
lastName = lName;
birthDate = new Date(bMonth, bDay, bYear);
hireDate = new Date(hMonth, hDay, hYear);
}

public String toString()
{
return lastName + ", " + firstName +
" Hired: " + hireDate.toString() +
" Birthday: " + birthDate.toString();
}
}

Using the Date class to define birthDate and hireDate is using composition.  Now you need to compile this with a similar command to get the class in the appropriate location as shown in the image above.

javac -d c:\jdk1.2.1\jre\classes Employee.java

Finally we want to test our new class that is partly based on the Date class.

Copy the following code into EmployeeTest.java and compile and run this application.  You should get some messages in your command window and an appropriate JOptionPane telling you the basic info on Bob Jones.

// EmployeeTest.java
// Demonstrating an object with a member object

import javax.swing.JOptionPane;
import edu.quinnipiac.javaProgs.time.Employee;

public class EmployeeTest
{
public static void main( String args[ ])
{
Employee e = new Employee ("Bob", "Jones", 7, 24, 49, 3, 12, 88);

JOptionPane.showMessageDialog(null, e.toString(), "Testing Clas Employee",
JOptionPane.INFORMATION_MESSAGE);

System.exit(0);
}
}