JamesMadisonUniversity

Computer Science Department


CS 159 Lab: Inheritance review


Objectives:

Students will:
  • create "related" classes to perform an action
  • identify similarities and differences among those related classes
  • use those similarities and differences to create a "family" of classes related by a common "parent".
  • 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)

super

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.txt HRV2.java
Acknowledgment Lab by Nancy Harris

1 General Instructions:

  1. Set up the environment for this lab.
  2. Download the Part 1 files in the Materials section.  DO NOT DOWNLOAD the Part 2 files yet.
  3. Answer as many of the following questions as you can. Be sure to think about the nature of inheritance as you answer the questions.

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 test driver for this application. 

  1. Open all three files (SalariedWorker.java, HourlyWorker.java, and HR.java) using your editor of choice.
  2. Look at what each method is doing.
  3. Compile and execute your HR.java to make sure it works.
  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. Open both files for editing.  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 variable.
  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. Alter the hourly worker pay method to calculate the pay as follows: (pays time and 1/2 for hours over 80).:
  10.  if(hours > 80)
      payAmt = (hours * rate) + ((hours - 80) * .5 * rate);
    else
    payAmt = hours * rate;
  11. Test this by passing in a larger number of hours to one of the two hourly worker's pay method. Correct any defects.
  12. 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.

UML

This UML describes the better solution that you will explore in this section. Notice the arrows pointing from the children back to the parent.

Download the 4 classes, Employee.java, SalariedWorkerV2.java, HourlyWorkerV2.java and HRV2.java.

  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.java?  Does it contain any attributes?  Does it also link to the Employee.java class?
  6. Review: 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 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.java, do you see any objects of the "Employee" type?
  8. In HRV2.java, we are building the more specific versions of Employee, either an 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?

7 Polymorphism:

  1. Copy HRV2.java to HRV3.java.
  2. Using the Polymorphism feature of Java (many forms) we could potentially get rid of the calls to individual objects if we could group them into an array since we are doing most of the processing in a group.
  3. Create an array of Employee at the beginning of the main method and instantiate it to 4 elements. Assign each of the objects (the SalariedWorkerV2 and HourlyWorkerV2) to a corresponding array element (0-3).
  4. Change each of the groups of processes so that instead of printing or paying individual employees we are printing or paying them all as a group using a for loop.
  5. Compile the HRV3.java program. What error do you get?
  6. Since the pay method is not in Employee, the compiler does not know about the pay methods. Add a pay method to Employee that returns 0. Compile the HRV3.java program. Do you get any errors?
  7. Run the HRV3.java program. Are the results correct (consistent with the first version)? Why or why not?
  8. How might you solve this problem? In other words, can you design a solution that will enable us to pay all employees at once?
  9. Be prepared to discuss in class next class period.

Turn in this assignment by the preferred method from your instructor.


Updated 04/10/2014 (nlh)