Inheritance


Classes with Shared Code

public interface SorterInterface
{
    public String getDescription();
    
    public void sort(ArrayList data);
}

Insertion Sorter Selection Sorter
public class InsertionSorter
implements SorterInterface
{
    // State:
    private String description;

    // Constructor
    public InsertionSorter()
    {
        this.description =
                    "Insertion Sort";
    }

    // Methods
    public String getDescription()
    {
        return this.description;
    }

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

    // Helper method for sorting.
    private int
    findInsertionPoint( ... )
    {
        // Same code we saw last week.
    }

}

public class SelectionSorter
implements SorterInterface
{
    // State:
    private String description;

    // Constructor
    public SelectionSorter()
    {
        this.description =
                    "Selection Sort";
    }

    // Methods
    public String getDescription()
    {
        return this.description;
    }

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

    // Helper method for sorting.
    private void swap( ... )
    {
        // Same code we saw last week.
    }

}


Alyce Brady, Kalamazoo College