JamesMadisonUniversity

Computer Science Department


CS 239 Lab 12: Motivation for Inheritance


Objectives:

Students will:

  • create "related" classes to perform an activity
  • identify similarities and differences among those related classes
  • use those similarities and differences to create a "family" of classes related by a common "ancestor".
  • begin to use the terminology of inheritance in java.

Background:

In this lab, you will work with two categories of employees, hourly workers and salaried workers.  For each payroll, hourly workers are paid based on the number of hours worked and their wage.  Salaried workers are paid the same wage each time they are paid.  Due to tax reasons, we also must keep some specific information about these workers for end of the year reporting.

New Terms:

inheritance (extends)

Refer to chapter 11.1 and 11.2 in Gaddis for a description of inheritance and the Java tutorial: http://java.sun.com/docs/books/tutorial/java/IandI/subclasses.html

Materials:

Part 1

Part 2

SalariedWorker.java

Employee.java

HourlyWorker.java

SalariedWorkerV2.java

HR.java

HourlyWorkerV2.java

Worksheet

HRV2.java

Acknowledgment

Lab by Nancy Harris modified slightly by Elizabeth Adams


1 General Instructions:

  1. Download the Part 1 files in the Materials section to your M drive in a folder named Lab 12.   DO NOT DOWNLOAD the Part 2 files yet.  You will be told when to download them later in the lab.
  2. Answer as many of the following questions as you can during lab. Be sure to think about the nature of inheritance as you answer the questions.
  3. When complete, upload your worksheet and your completed .java files.
  4. This assignment must be submitted  by midnight tonight.
  5. Please bring a print out of your worksheet AND your files to class tomorrow.

2 Creating two related files:

In this part of the lab, you will work with two categories of employee, a salaried worker and an hourly worker.  The HR.java is your driver for this application. 

  1. Open all three files (SalariedWorker, HourlyWorker, and HR.java) using your preferred editor of choice.
  2. Compile and execute your driver to make sure it works.
  3. Notice that the pay() and toString methods are shells.  Add the code to SalariedWorker and HourlyWorker to pay each of these employees and fill in the toString as specified in the method header.
  4. Run the HR.java and insure that the methods are working correctly.

4 Looking for patterns:

This part of the lab will help you understand why we might want to use inheritance.

Look at the SalariedWorker and HourlyWorker classes:

  1. What attributes are the same between the Salaried and Hourly workers?  Are there any that are different?
  2. What methods are the same in name between the Salaried and Hourly workers?
  3. Which methods do the same thing?  Which methods do different things?
  4. Edit both files.  The Payroll department has decided to add a new field, homeDepartment (a String) to the worker classes and to print out that new field as part of the toString which shows the payroll amount.  Add this field as an instance attribute.
  5. Alter the constructors to accept this value as a parameter and to store it in the new field.
  6. Alter the toString methods to print this value directly following the SSN of the employee.
  7. Compile and test your solution, altering the HR program to pass a department name into the constructor.
  8. How many individual statements did you need to add to SalariedWorker and HourlyWorker collectively?
  9. Finally, alter the hourly worker pay method to pay the worker up to 80 hours at their normal wage rate.  Any hours over and above 80 hours should be paid at time and 1/2.  (1.5 * their normal rate).  Test this by passing in a larger number of hours to one of the two hourly workers and correct any issues.
  10. Did this change require any changes to your SalariedWorker class?

5 A better solution:

This part of the lab will help you to understand what inheritance in java is and how you implement inheritance.

Download the 4 classes, Employee.java, SalariedWorkerV2.java, HourlyWorkerV2.java and HRV2.java  from Part 2 above. 

  1. Look at the list of instance variables (attributes) that you created in question 4.1.  In which class are these located?
  2. Are these attributes found anywhere in any of the other three classes?
  3. Look at the SalariedWorkerV2.java class.  Does it contain any attributes of its own?
  4. What do you see that might "link" SalariedWorkerV2.java to the Employee.java class?
  5. How about HourlyWorkerV2?  Does it contain any attributes?  Does it also link to the Employee.java class?
  6. The extends keyword in java tells the compiler that this class is related to a parent.  The parent is identified in the extends clause in the class header.  We say that SalariedWorkerV2 and HourlyWorkerV2 are children of or subclasses of the Employee.java class.  Employee.java is the superclass or parent of the other two.  Both SalariedWorkerV2 and HourlyWorkerV2 are specializations of the general employee class and we can also say that SalariedWorkerV2 is-a Employee and HourlyWorkerV2 is-a Employee..
  7. In HRV2, do you see any objects of the "Employee" type?
  8. In HRV2, we are building the more specific versions of Employee, the HourlyWorkerV2 or SalariedWorkerV2.  Looking at the methods in Employee, what method does not exist in Employee.java that does exist in each of the two specializations?
  9. What methods exists in Employee.java that do not exist in each of the two specializations?
  10. Does this make sense?  Why or why not?
  11. Notice that the HourlyWorker and SalariedWorker constructors accept the same parameters as they did before.  Somehow we need to transmit those parameters to the Employee class where the attributes are actually located.  super is the keyword to identify the parent class just as this is used to identify a class within itself.  You call the parent constructor by calling super and passing in the appropriate parameters.  Do this for the SalariedWorkerV2 and HourlyWorkerV2 classes. 
  12. Compile the superclass and each of the subclasses.  Compile the driver and test.  Notice that the toString works even though we have not created an Employee object.  Each of the two subclasses use its parent's method unless they have one of their own.
  13. Notice that pay() has not been defined for either of the subclasses.  Create appropriate pay() methods based on your final work with SalariedWorker.java and HourlyWorker.java.  Compile and test.

6 Extending the inheritance:

In part 4, you added a field to each of the classes.  

  1. Where would you add this same field to your Employee.java family of classes?  Where would you change the toString()?  What constructors do you need to change?
  2. Do this and test your solution.
  3. Does inheritance save you any coding?  What would you say are the advantages and disadvantages of using an inheritance structure for a related set of classes?

Upload a completed copy of each of your files to this Blackboard assignment.


Updated 09/30/2008 (esa)