Inheritance


Subclass Examples

Subclass 1 Subclass 2
public class InsertionSorter
extends SorterAbstClass
{
  // State:
  //     inherit from SorterAbstClass;
  //     no new state in this case

  // Constructor

    /**
     * Constructs new object of class.
     */
    public InsertionSorter()
    {
        // Initialize inherited state
        // using superclass constructor.
        super("Insertion Sort");

        // no extra init. necessary
    }

  // Methods

    // getDescription is inherited

    /**
     * Sorts data in ArrayList.
     */
    public void sort(ArrayList data)
    {
        // Same code we saw last week.
        //   ...
    }

    /**
     * Helper method for sorting.
     */
    private int
    findInsertionPoint(ArrayList data,
      String comparison, int startIndex)
    {
        // Same code we saw last week.
        //   ...
    }

}

public class SelectionSorter
extends SorterAbstClass
{
  // State:
  //     inherit from SorterAbstClass;
  //     no new state in this case

  // Constructor

    /**
     * Constructs new object of class.
     */
    public SelectionSorter()
    {
        // Initialize inherited state
        // using superclass constructor.
        super("Selection Sort");

        // no extra init. necessary
    }

  // Methods

    // getDescription is inherited

    /**
     * Sorts data in ArrayList.
     */
    public void sort(ArrayList data)
    {
        // Same code we saw last week.
        //   ...
    }

    /**
     * Helper method for sorting.
     */
    private void swap(ArrayList data,
      int index1, int index2)
    {
        // Same code we saw last week.
        //   ...
    }

}


Alyce Brady, Kalamazoo College