CS1020 -- Lab 3: Inheritance and Polymorphism

Winter 2004
Objectives Addressed
If you need help with any exercise, raise your hand.

Download and unzip the lab3.zip file.

Select "Switch Workspace..." in Eclipse and select the newly unzip'd lab3 directory.

Issues with People and Dogs

Begin in the Mammals project and open the Dog class. Try to compile this class. There is a problem that you must identify and fix by editing the Mammal class. Once fixed, maintain this fix throughout the remainder of the lab.

Examine the classes Mammal and Human as well. Draw a UML class diagram to show the relationship among these three classes.

Checkpoint 1

When you are ready, call me over and explain the problem you encountered compiling the Dog class and how you fixed it. Also, show me your class diagram.

Demo 1

Examine the Demo1 class in the same project. Predict the output when you compile and run this program. Now compile and run it. How did the hair color get assigned?

Checkpoint 2

When you are ready, call me over and explain what happens when you run the Demo1 class.

Demo 2

Edit the Demo2 class in the same project. Predict the output when you compile and run this program and then do so. Make sure you understand its behavior, and then change the statement

	Human [] human = new Human[SIZE];
to
	Mammal [] human = new Human[SIZE];
and try to compile this program. What happens? What is the problem?

Leaving this change in place, change the statement

            System.out.println(human[i].getSSN());      
to
            System.out.println(human[i].getHairColor());      
and try to compile the program. What happens?

To help you understand this, draw a picture of the array called human and think about what the objects in this array are declared to be.

Checkpoint 3

When you are ready, call us over to explain what happens when you make the above changes to Demo2.

Demo 3

Examine class Demo3. What do you think will happen when you compile and run it? Do so and run it several times. Is this an example of polymorphism?

Checkpoint 4

When you are ready, call me over and explain what happens when you run this program and whether or not it is an example of polymorphism.

Demo 4

Finally, examine class Demo4. What do you think will happen when you compile and run it? Do so and run it several times. Is this an example of polymorphism?

Checkpoint 5

When you are ready, call me over and explain what happens when you run this program and whether or not it is an example of polymorphism.

Access to Members
Demo1

Now turn your attention to the Accessible project and examine the classes Q and X. Observe that an instance of the class X contains an instance of the class Q as a data member.

Examine the class Demo1 and predict what will happen when you run this program. Compile and run Demo1 and verify that your predictions are correct.

Now un-comment the two lines in the main method of the Demo1 class. The purpose of un-commenting this code is to increment the internal value of the Q object before printing its value a second time. Compile this class and observe the error message.

There are two ways to correct the error. One is to change the visibility of the data member q in the definition of the class X, and another is to have the Demo1 class access this data member through the accessor built in to the class X. In each case, you should modify only one line in the file.

Implement both of these ways and compile and run your program. Be sure to restore your class X to its original contents before you make your changes to Demo1 before you recompile Demo1.

Checkpoint 6

When you are ready, call me over and explain your changes and show me your running programs.

Demo2

Next examine the class Demo2. Try to compile this class and observe the error message you get.

There are two ways to correct the error. One is to change the visibility of the data member q in the definition of the class X to protected. Make this change and compile your Demo2program. Verify that there is no longer an error. After you have done this, restore your class X to its original contents (by making the data member q private).

Another way to correct the error is to keep q private in the class X but to let the class Y access the data member q through the getQ() method in its parent class X. You can do this one of two ways:

Verify that each of these changes to Y result in error-free compiles and correct output. Be prepared to explain why either of these changes solves the problem.

Checkpoint 7

When you are ready, call me over and explain why either of the two changes solves the problem.

Acknowledgment

This laboratory assignment is based on a similar assignment developed by Susan Haller and Timothy Fossum at UW-Parkside.

Last Updated: Thursday, 01-Jan-2015 13:30:04 CST