Inheritance and Polymorphism

Work on this exercise by yourself, but ask questions of your instructor, student assistants and classmates as desired.

Goals

The goals of this exercise are to:

The Exercise

Instructions Part 1: Download the project from your individual repository

Instructions Part 2: Understand the basic code.

  1. Go over the Employee and HourlyEmployee classes with your instructor.
  2. Run InheritanceDemo.java. (Note that the Polymorphism.java file won't compile until after you finish Part 3.)
  3. Make sure you understand:
  4. If there was a quiz question related to the demo, you should do it now ... feel free to test your code here, but you will need to write out your solution on the quiz.

Instructions Part 3: Create a SalariedEmployee from scratch.

A SalariedEmployee is an Employee that has an annual salary.
  1. Create a SalariedEmployee class that extends Employee.
  2. Create the same constructors: a default that sets the salary to 0; a 3-arg one that takes and name, date, and salary; and a copy constructor that creates a copy of a SalariedEmployee object that's passed to it. (A copy constructor is an alternative to clone.)
  3. Write the following methods: getSalary() accessor, getMonthlyPay() (like was done for the hourly employee), toString(), and .equals().
  4. Modify the InheritanceDemo.java file to create a SalariedEmployee and test some of its methods.

Instructions Part 4: Polymorphism

  1. Run PolymorphismDemo.java.  Look at the code. How we are using polymorphism here?
  2. Add code to print the pay of each employee that has pay. In this simple version, you won't need to use typecasting or instanceof.
  3. Show your code to the instructor or an assistant.