Experiment 1: Abstract Classes


Study the source program BasicAccount.java in the AccountKinds folder, particularly the lines

   package AccountKinds;

   public abstract class BasicAccount {

Notice that in addition to declaring the class to be public, we made it abstract, too.

Now look at the code in AccountDemo.java where we create a new account.

   theScreen.print("\nCreating a Regular Account: ");
   RegularAccount anAccount;
   anAccount = new RegularAccount("The green hornet", "1202", 50.0);

In the space below, write whether you think this will create a compile time error or not.





Compile the project, and run (execute) the resulting program. Was there an error?





If what happened was what you anticipated, give a detailed explanation of why you said what you did. If what happened differs from what you anticipated, explain what was wrong with your thinking.





A Simple Change

Change the code in AccountDemo.java to be:

   theScreen.print("\nCreating a Regular Account: ");
   BasicAccount anAccount;
   anAccount = new BasicAccount("The green hornet", 50.0);

In the space below, write whether you think this will create a compile time error or not.





Compile the project, and run (execute) the resulting program. Was there an error?





If what happened was what you anticipated, give a detailed explanation of why you said what you did. If what happened differs from what you anticipated, explain what was wrong with your thinking.





This illustrates the effect of making a class abstract.

 

Handles

Change the code in AccountDemo.java to be:

   theScreen.print("\nCreating a Regular Account: ");
   BasicAccount anAccount;
   anAccount = new RegularAccount("The green hornet", "1202", 50.0);

In the space below, write whether you think this will create a compile time error or not.





Compile the project, and run (execute) the resulting program. Was there an error?





If what happened was what you anticipated, give a detailed explanation of why you said what you did. If what happened differs from what you anticipated, explain what was wrong with your thinking.





In this example, the variable anAccount is a handle for an object which is an instance of BasicAccount or any of its subclasses.


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.