Laboratory: Inheritance
CSC 207 Algorithms and Object Oriented Design Spring 2009

Summary: In this lab, you will explore inheritance in Java by building and extending some simple classes.

Preparation

  1. Create a directory for this lab:
      mkdir somewhere/inheritance
  2. Go to your new directory:
      cd somewhere/inheritance
  3. Copy the file for this lab to your new directory:
       cp ~weinman/public_html/courses/CSC207/2009S/labs/code/inheritance/WhatCounts.java .

Exercises

Exercise 1

To start, we will create a class called Counter that will allow clients to build objects that count things, starting at some value.

The class should contain

When you are finished, inspect the class WhatCounts.java that you copied in the lab preparations.

Exercise 2

  1. Create a new class, DecrementableCounter, that has the following form:
    DecrementableCounter.java
    public class DecrementableCounter
      extends Counter
    {
      public DecrementableCounter(int start)
      {
        super(start);
      } // DecrementableCounter(int)
    } // class DecrementableCounter
  2. Change the initialization of gamma in WhatCounts.java so that it reads
    Counter gamma = new DecrementableCounter(-5);
  3. What effect to you expect this change to have? Confirm or refute your answer experimentally.
  4. Add a decrement() method to DecrementableCounter This method should subtract one from the count field.
  5. Add lines to WhatCounts to test that method.
  6. Change the initialization of gamma so that it reads
    DecrementableCounter gamma = new Counter(-5);
  7. What effect to you expect this change to have? Confirm or refute your answer experimentally.
  8. Restore the initialization of gamma to
    Counter gamma = new DecrementableCounter(-5);

Exercise 3

  1. Create a new class, NamedCounter, that has the following form
    NamedCounter.java
    public class NamedCounter
      extends Counter
    {
      String name;
      public NamedCounter(String name, int start)
      {
        super(start);
        this.name = name;
      } // NamedCounter(String, int)
    } // class NamedCounter 
  2. Update WhatCounts so that the initialization of alpha reads
    Counter alpha = new NamedCounter(0);
  3. What effect do you expect this change to have? Confirm or refute your prediction experimentally. If necessary, fix your declaration of alpha as a NamedCounter.
  4. Run your current version of WhatCounts.
  5. Override the toString method by inserting the following code into NamedCounter
    
      public String toString() {
        return this.name + super.toString();
      } // toString()
    
  6. What effect do you expect this change to have? Confirm or refute your prediction experimentally.
  7. Swap the two lines in the constructor for NamedCounter and determine what errors, if any, you get. Why do you think that is?
  8. Restore the constructor.

Exercise 4

  1. Create a new class, DoubleCounter, that has the following form:
    DoubleCounter.java
    public class DoubleCounter
      extends Counter
    {
    
    } // class DoubleCounter 
  2. Compile your new class. What error messages, if any, do you receive?
  3. Insert a constructor for DoubleCounter of the following form
    
      public DoubleCounter(int start)
      {
        super(start);
      } // DoubleCounter(int)
    
  4. Update WhatCounts so that the initialization of beta reads
    Counter beta = new DoubleCounter(123);
  5. What effect do you expect this change to have? Confirm or refute your prediction experimentally.
  6. Override the increment method by inserting the following code into DoubleCounter
    
      public void increment()
      { 
        super.increment();
        super.increment();
      } 
    
  7. What effect do you expect this change to have? Confirm or refute your prediction experimentally.

Exercise 5

  1. Create a subclass of Counter called LimitedCounter that includes
    • an int field named limit;
    • a constructor that takes two parameters: a starting value and an upper limit (that is, a value for the limit field); and
    • a modified increment method that throws an exception when count exceeds limit.
  2. Determine the results of changing the initialization of gamma to
    Counter gamma = new LimitedCounter(-5,3);

Exercise 6

  1. 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.)
  2. What effect, if any, do you expect that change to have? Confirm or refute your results experimentally.
Created: Samuel A. Rebelsky, 2 March 2006
Adopted from Espresso: Laboratory: Inheritance