CS Labs: Lab 13

CS Labs: Lab 13


To accompany Chapter 13 of An Introduction to Object-Oriented Programming with Java by C. Thomas Wu.

Inheritance and Polymorphism

There are 8 checkpoints in this lab. If you need help with any exercise, raise your hand.

Copy the lab materials to your account by entering

             cp -r /home/Classes/Cs1/Labs/Lab13 .
     
Everything you need for the lab exercises today is contained in this new directory.

Issues with People and Dogs

Change into subdirectory Mammals and edit the Dog class. Try to compile this class. There is a problem that you must identify and fix by editing the Mammal class. Maintain this fix throughout the lab.

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

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

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

2 When you are ready, call us over to explain what happens when you run the Demo1 class.

Edit the Demo2 class in the same subdirectory. 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.

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

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?

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

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?

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

Accessing Members (or not)

Go into the Accessible subdirectory 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, and remove all your .class files before you recompile Demo1.

6 When you are ready, call us over to explain your changes and show us your running programs.

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) and remove the X.class file.

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.

7 When you are ready, call us over to explain why either of the two changes solves the problem.

After the Lab

8 Show us that you have logged out, cleaned up, and pushed in your chairs for this last checkpoint.

End of Lab


Susan Haller and Timothy Fossum, University of Wisconsin-Parkside