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
Instructions Part 1: Download the project using Subclipse
This is a new way of downloading demo projects that should work well. All the sample code is in a repository; you'll just connect to it and grab the project.
- In Eclipse, go to the SVN Repository Exploring View.
- Right-click in the SVN Repository window on the left (anywhere, but not on an existing repository), and choose New > Repository Location ...
- For the URL, type:
http://svn.cs.rose-hulman.edu/repos/csse221-200810-public
(Note, it might be easiest to modify an existing location using the drop-down menu.)
- Expand the repository that comes up and select the InheritanceDemo project, right-click, and choose Checkout
- Change back to Java Perspective
- Choose Team > Disconnect ... and choose the option to delete the svn meta info. This will make your copy of this project standalone,
so you don't modify the shared online copy by accident.
- Optional: You may choose Team>Share Project to YOUR personal repository if you want version control for this project.
Since this project won't be turned in, that's your decision.
Instructions Part 2: Understand the basic code.
- Go over the Employee and HourlyEmployee classes with your instructor.
- Run InheritanceDemo.java. (Note that the Polymorphism.java file won't
compile until after you finish Part 3.)
- Make sure you understand:
- why HourlyEmployee doesn't have a getName() method
- what super() does
- 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: Create a SalariedEmployee from scratch.
A SalariedEmployee is an Employee that has an annual salary.
- Create a SalariedEmployee class that extends Employee.
- 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.
- Write the following methods: getSalary() accessor, getPay() (that gets
monthly pay, like the hourly employee did), 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. This is where
you'll need typecasting and instanceof.
- Show your code to the instructor or an assistant.