CSC 207 Grinnell College Fall, 2014
Algorithms and Object-Oriented Design

Example with Class and Interface Inheritance


This laboratory provides practice in extending one class while implementing an interface.


This lab has been motivated by Exercise 4.51 in the book Data Structures and Problem solving Using Java, Fourth Edition by Mark Allen Weiss. However, elements of this lab involving Java's Comparable interface and sorting by call number are not part of Weiss' exercise, and other parts of Weiss' original questions have been changed.


This lab considers a class hierarchy that models books in a library. Although the model is streamlined to aid implementation for this lab, the approach could be expanded and refined to yield an extensive application.

The main elements of the framework for books is shown in the following.

book class hierarchy

The following comments provide a high-level overview of these classes.

The Book class and its subclasses

Documentation for the functionality of these classes is given in the javadoc documentation. A more detailed description of these classes follows.

Class Book

Class Book models information common to all books. Any book has

Within a Book class and its subclasses, these fields might be used directly, but processing by other classes and objects should be done via getters and setters.

Class LibraryBook

Class LibraryBook models information common for library books. In addition to author, title, and ISBN, library books have call numbers, and library books are stored on the shelves in order by call number. Further, many library books may be able to circulate. In addition to fields and methods of books, any library book has

Since checkout, returned, and circulationStatus depend upon subclasses (i.e., whether book is circulating or in the reference collection), these methods are abstract — identified but not implemented.

Although library books can be ordered by call number, these call numbers are not present in the Book. Thus, LibraryBook can implement Java's Comparable interface, but Book cannot.

Since some methods are defined but not implemented, LibraryBook is an abstract class.

Class ReferenceBook

Class ReferenceBook models a reference book. Reference books are housed in a specific collection (e.g., the Iowa Room, Burling, or Science), and reference books do not circulate.

In addition to fields and methods of library books, a reference book has

With checkout, returned, and circulationStatus implemented for LibraryBook, this is a fully-implemented class.

Class CirculatingBook

Class CirculatingBook models books that can circulate. When checked out, information is stored about the patron who borrowed the book and when it is due. In addition to fields and methodds of library books, a circulating book has

Although production-level software would include substantial error checking, the implementation for this lab may involved minimal error checking. That is, it is not necessary to check that the book has not been checked out when calling checkout or that the book has been checked out when calling returned.

Consequences of Interface Comparable

Many Java classes rely upon Java's Comparable interface. For example, the Collections class contains methods to sort and search an ArrayList of Comparables. Thus, by defining a compareTo method, applications involving the LibraryBook class can use many operations with no additional work!

Work for this Lab

Implement the classes identified in this narrative, following the above notes and the relevant javadoc docuentation.

Although this lab requires the implementation of four classes, each class can be reasonably short. For example, in preparing this lab, I implemented and documented these four classes, and my notes are in the following table.

Class/Interface Time to Create Code Time to Document with javadoc,
other comments
Class Book 4 minutes 3 minutes create package, class, fields, constructor, toString, all getters and setters
to create getters and setters in Eclipse, go to Source tab and click "Generate Getters and Setters"
Interface Comparable 0 minutes 0 minutes Predefined at Java's Comparable interface
abstract Class LibraryBook 5 minutes 3 minutes create class with call number field, constructor, getters and setters, circulationStatus, compareTo, toString
the abstract method circulationStatus will be set in subclasses, but it can be used in the toString method
Class ReferenceBook 10 minutes 3 minutes create class with collection field, constructor (mostly calls super, getters and setters, specify circulation status is always "non-circulating reference book", add collection string to toString
Class CirculatingBook 8 minutes 3 minutes create class with currentHolder and dueDate fields, getters and setters, provide for book checkout and bookReturned, clarify circulationStatus based on whether or not book is checked out
Class Library 60 minutes 15 minutes create class, test data, check searching and sorting
most development time devoted to writing tests
shell for this class supplied, so your testing can begin with some test cases already identified


As the table shows, the book-related classes can be implemented in a reasonable amount of time. However, I got carried away in putting together test cases, so my implementation of a Library testing class took over an hour. For reference, I also include output from running

For the Library testing class, work on main took a substantial amount of time. The other capabilities for a library, however, went quite quickly.

Although the full Library class is given to allow the start of testing for this lab, be sure you understand how this code works — tests will assume knowledge of this material.

Work to Turn in

This document is available on the World Wide Web as

created 15 February 2012
revised 23 February 2012
revised for Java 7 30 September 2014
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at