

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


Fall 2005 Lab 12 – Inheritance Handout
1. Lab Objectives Following the lab you should be able to: Explain what inheritance is, why it is used and when it is used. Identify the superclass and subclasses in a given program. Understand the meaning of the keywords: abstract , protected , extends , and super , and know when and how to use them. Write abstract classes and abstract methods. Explain the override property of inheritance. Define a reusable class based on inheritance. Derive new subclasses from a superclass to extend a solution to new problems. Design and give an inheritance solution for a problem. 2. Prior to the laboratory Review the laboratory handout. Review Wu, Chapter 13. Read the tutorial, “Understanding Inheritance” http://java.sun.com/docs/books/tutorial/java/javaOO/classvars.html Take the lab pretest 3. Topics Topic Basic concepts of inheritance When and why inheritance is used Member accessibility: public, private, protected Inheritance and Constructors – Super() Abstract super classes and abstract methods Method overriding 4. Activities/Exercises 1. Understand the concept of inheritance and become familiar with the syntax of writing programs using inheritance. 2. Add override methods to an existing program. 3. Add new subclasses to an existing program. 4. Write a simple program that uses inheritance. Activity 1: Understand the concept of inheritance and become familiar with the syntax of writing programs using inheritance Download Item.java, Flashcard.java, FlashcardTest.java from the class website (in the Inheritance/Flashcard folder). Before compiling and running the program, open Item.java, Flashcard.java, FlashcardTest.java. o Identify the superclass and subclass(es).
Fall 2005 o Identify which methods belong to the superclass, which methods belong to each subclass, and which methods belong to both classes. Complete Question 1 on your worksheet for Activity 1. Compile and run the program “FlashCardTest”. Complete Question 2 on your worksheet for Activity 1. Activity 2: Add override methods to an existing program Download Mood.java, MoodyObject.java, HappyObject.java, SadObject.java, and MoodTest.java from the class website (in the Inheritance/Mood folder). Before compiling and running the program, open Mood.java, MoodyObject.java, HappyObject.java and SadObject.java. Identify the superclass and the subclass(es). Notice that there are several levels of inheritance relationships. Complete Question 1 on your worksheet for Activity 2. Compile and run the program “MoodTest.java”. Observe the program output. Do the results match your understanding of the inheritance relationship? Complete Question 2 on your worksheet for Activity 2. Think about whether the Mood class should be an abstract or a non-abstract class. Complete the first part of Question 3 on your worksheet for Activity 2. Modify the Mood class to make it an abstract class. Compile and run the new version of the program. Complete Questions 3 and 4 on your worksheet for Activity 2. Add a new abstract method called void behave( ) to the Mood class. All of the subclasses should override this method. Change the method named speak() in MoodyObject to behave(), change the method named laugh () in HappyObject to behave(); change the method named cry () in SadObject to behave(). Now, all three subclasses have the same behave() method. Compile and run the new version of the program. Complete Question 5 on your worksheet for Activity 2. Activity 3: Add new subclasses to an existing program Download SmartGuysInc.java, StaffMember.java, Employee.java, and Hourly.java from the class website (in the Inheritance/SmartGuysInc folder). In this activity, you are required to add a new subclass to the existing program. o The SmartGuysInc module computes and displays a list of “Smart” guys. o The SmartGuysInc class is the test program that provides the execution entry. o The StaffMember class represents a superclass of all the personnel classes in the SmartGuysInc module. It is an abstract class with a data member name, an abstract method, pay( ), and a public method, print(). o The Employee class extends the StaffMember class and overrides the pay() method and modifies the print() method. Employee class also has a new date member payRate which represents the salary rate of the employee. o The Hourly class extends the Employee class. It inherits all of Employee’s data members and methods. It has its own method, addHours( ), because hourly employees receive their salary based on how many hours they work. Create a new a new class called Executive.