Experiment 3: Subclasses - Inheriting Methods


Part 1

When an object gets a message, it will start at the outside and look for the first layer that implements a method for that message. In other words, we start at the class of the object and work our way up the hierarchy looking for the method. If we get all the way to Object (the root of all class hierarchies) without finding the method, an error will be generated.

Consider the following code:

   theScreen.println("The account is owned by " + anAccount.name());

Look at the code for each of the superclasses of RegularAccount and predict what result it will have.





Add the line of code to AccountDemo.java and record your result here.





If it was different from your prediction explain why.





Part 2

Now consider the following lines of code:

		theScreen.println("Depositing 100.00 ");
		anAccount.deposit(100.00);
		theScreen.println("\nAccount status is: " + anAccount);

Predict what result it will have.





Add the code to AccountDemo.java and record your result here.





If it was different from your prediction explain why.





Part 3

Look over the classes again, if needed, and write a line of code that you believe will cause 100.00 to be deposited into the account. (Hint: A RegularAccount is-a ProtectedAccount.)

 

 

Revise and debug until you are successful.


Back to the Exercise List

Forward to the Next Experiment



Back to the Table of Contents

Back to the Introduction


Copyright 2000 by Prentice Hall. All rights reserved.