CSSE 221: Fundamentals of Software Development Honors
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:
- Apply concepts of inheritance
- See polymorphism at work using arrays.
The Exercise - Overview
You will:
- Check out a simple program named InheritanceDemo from your repository, by using Subclipse.
- Read the program and answer explore questions about it.
- Make some easy modifications to the program.
Instructions, Part 1 - Checkout the Project
- Open Eclipse.
- Checkout your InheritanceDemo project.
Instructions, Part 2 - Understand the basic code
- Go over the Employee and HourlyEmployee classes with your instructor.
- Run InheritanceDemo.java.
- Make sure you understand:
- why HourlyEmployee doesn't have a getName() method
- what super() and super(name, date) do
- how to use the other version of super (super.someMethod() )
- 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 - Examine the SalariedEmployee class
A SalariedEmployee is an Employee that has an annual salary.
- We created a SalariedEmployee class that extends Employee.
- We 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.)
- Study the getMonthlyPay() accessor (it should work like the one in the hourly employee class).
- Write the following methods: getSalary() accessor, toString(), and
.equals().
- Modify the InheritanceDemo.java file to create a SalariedEmployee and test some of its methods.
Instructions, Part 4 - Polymorphism
- Run PolymorphismDemo.java. Look at the code. How we are using polymorphism here?
- 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.
- Show your code to the instructor or an assistant.