Design - Abstract Classes and Interfaces


Terminology

abstract class
A class without instances - an abstract class may have attributes and concrete methods.
interface
A set of services provided by a class

Guidelines

abstract class
You have several classes providing the same functionality in the same way. The parent is a very generalized version of the children, so it doesn't make sense to build concrete cases of one. Ex: Car - We talk about driving cars, but we are really driving very specific instances of cars. There is no "car" objects, they are sedans, station wagons, vans, etc. You can even get down to specific model numbers.
interface
You have classes that provide the same functionality in different ways and/or the classes should not be related hierarchically.

Java examples

abstract class
AbstractButton: A generic button-like GUI object. It can be pushed and can respond to that push. It's instances will be more specific buttons.

AbstractCollection: A skeletal implementation of a class the contains a set of other objects.

interfaces
Comparable: Objects that can be ordered.

ActionListener: Objects that can be performed of GUI actions (button clicks, ex)

Today's exercise

The problem

You have been asked to design a system for keeping track of the JMU Library's collection. The "collection" includes material that is available to patrons of the library. There are several different types of materials that they are interested in cataloging. There are books, videos (both VHS and DVD), audio CD's, periodicals (we are concered about hard copy only), and art.

Books are of different kinds: fiction, non-fiction, and reference which is a special category of non-fiction. All but the reference books may be circulated. All but the reference books may be placed on reserve. The same is true of videos, audio CDs. Art and periodicals may not be circulated or reserved.

All materials will be "located" somewhere in the library. At any point for a given item, we should be able to display where in the library the item can be found. For items on reserve, the word "Reserves" should print; for items in circulation, the word "Out" should print along with the date expected back. Items are also searchable; they will all have a set of keywords associated with the item that can be searched by users of these classes.

Our focus here is on designing the collection in such a way that we can carry out all of these requirements efficiently. We are not concerned about the entire application at this stage.

The design

There are many ways to design this system. We could approach it from an "entity" point of view. What are the items in the collection and what are the capabilities of each. We could also approach it from a "capability" point of view and decide to group items based on their capabilities.

To be continued...


CS 239 - Spring 2006
Department of Computer Science Nancy Harris Home Page Current Classes Link