Espresso: A Concentrated Introduction to Java


Laboratory: Inheritance

Summary: In today's laboratory, you will explore inheritance in Java by building and extending some simple classes.

Equipment

Contents

Exercises

Exercise 0: Preparation

You will continue to work with project Code and the package username.util.

Exercise 1: Your Base Class

Write a class, Counter, in package username.util. The class will allow clients to build objects that count things, starting at some value.

The class should contain

Here is a simple tester for that class.

package username.util;

import java.io.PrintWriter;

public class WhatCounts
{
  public static void main(String[] args)
    throws Exception
  {
    PrintWriter pen = new PrintWriter(System.out, true);
    Counter alpha = new Counter(0);
    Counter beta = new Counter(123);
    Counter gamma = new Counter(-5);
    pen.println("Original version of alpha: " + alpha.value());
    pen.println("Original version of beta: " + beta.value());
    pen.println("Original version of gamma: " + gamma.value());
    for (int i = 0; i < 10; i++) {
      pen.println("Incrementing ...");
      try { alpha.increment(); }
      catch (Exception ea) { pen.println("Failed to increment alpha."); }
      try { beta.increment(); }
      catch (Exception ea) { pen.println("Failed to increment beta."); }
      try { gamma.increment(); }
      catch (Exception ea) { pen.println("Failed to increment gamma."); }
    } // for
    pen.println("Incremented version of alpha: " + alpha.value());
    pen.println("Incremented version of beta: " + beta.value());
    pen.println("Incremented version of gamma: " + gamma.value());
    alpha.reset();
    beta.reset();
    gamma.reset();
    pen.println("Reset version of alpha: " + alpha.value());
    pen.println("Reset version of beta: " + beta.value());
    pen.println("Reset version of gamma: " + gamma.value());
  } // main(String[])
} // class WhatCounts

Exercise 2: Decrementable Counters

a. Create a new class, DecrementableCounter, that has the following form:

public class DecrementableCounter
  extends Counter
{
  public DecrementableCounter(int _start)
  {
    super(_start);
  } // DecrementableCounter(int)
} // class DecrementableCounter

b. Change the initialization of gamma so that it reads

    Counter gamma = new DecrementableCounter(-5);

c. What effect to you expect this change to have? Confirm or refute your answer experimentally.

d. Add a decrement() method to DecrementableCounter This method should subtract one from the count field.

e. Add lines to WhatCounts to test that method.

f. Change the initialization of gamma so that it reads

    DecrementableCounter gamma = new Counter(-5);

g. What effect to you expect this change to have? Confirm or refute your answer experimentally.

h. Restore the initialization of gamma to

    Counter gamma = new DecrementableCounter(-5);

Exercise 3: Naming Counters

a. Create a new class, NamedCounter, that has the following form

public class NamedCounter
  extends Counter
{
  String name;
  public NamedCounter(String _name, int _start)
  {
    super(_start);
    this.name = _name;
  } // NamedCounter(String, int)
} // class NamedCounter 

b. Update WhatCounts so that the initialization of alpha reads

    Counter alpha = new NamedCounter(0);

c. What effect do you expect this change to have? Confirm or refute your prediction experimentally.

d. Override the toString method by inserting the following code into NamedCounter

  public String toString()
  {
    return this.name + super.toString();
  } // toString()

e. What effect do you expect this change to have? Confirm or refute your prediction experimentally.

f. Swap the two lines in the constructor for NamedCounter and determine what errors, if any, you get.

g. Restore the constructor.

Exercise 4: Double Counters

a. Create a new class, DoubleCounter, that has the following form

public class DoubleCounter
  extends Counter
{
} // class DoubleCounter 

b. What error messages, if any, do you receive?

c. Insert a constructor for DoubleCounter of the following form

  public DoubleCounter(int _start)
  {
    super(_start);
  } // DoubleCounter(int)

d. Update WhatCounts so that the initialization of beta reads

    Counter beta = new DoubleCounter(123);

e. What effect do you expect this change to have? Confirm or refute your prediction experimentally.

f. Override the increment method by inserting the following code into DoubleCounter

  public void increment()
  { 
    super.increment();
    super.increment();
  } // increment()

g. What effect do you expect this change to have? Confirm or refute your prediction experimentally.

Exercise 5: Limited Counters

a. Create a subclass of Counter called LimitedCounter that includes

b. Determine the results of changing the initialization of gamma to

  Counter gamma = new LimitedCounter(-5,3);

Exercise 6: Limited Counters, Revisited

a. Make LimitedCounter a subclass of DoubleCounter rather than a subclass of Counter. (That is, change the line that reads extends Counter to read extends DoubleCounter.)

b. What effect, if any, do you expect that change to have? Confirm or refute your results experimentally.

History

Thursday, 2 March 2006 [Samuel A. Rebelsky]


This page was generated by Siteweaver on Thu Mar 30 15:24:37 2006.
The source to the page was last modified on Thu Mar 2 23:19:22 2006.
This page may be found at http://www.cs.grinnell.edu/~rebelsky/Espresso/Labs/inheritance.html.

You may wish to validate this page's HTML ; Valid CSS! ; Check with Bobby

Samuel A. Rebelsky
rebelsky@grinnell.edu