Copyright © 1998 Sita Ramakrishnan, Monash University All rights reserved

Light Views

HomeCase StudiesOO TheoryHelp

Beaches and Parking Case Study

Inheritance and Collaboration

To understand how the case study uses object oriented technology in its implementation, the inheritance and the collaboration of the classes ought to be looked at. Inheritance represents `is - a' relationship, this simply means if class B inherits from class A then B can be referred to as B was class A. This type of relationship is also referred as parent/child relationship. It allows the programmmer to re-use existing software blocks or objects and incorporate new changes to them to adapt them to new environment. The following are just some of the benefits of inheritance:

  • re-use, productivity is increased;
  • refine the child class for specific problem;
  • extends the functionlity of the current object.

Collaboration explains the dependency of the classes. It shows which classes have other classes as their data member or uses them somewhere in their class definition. Collaboration and client/server terms are used interchangeably in OO environment. The term client/server implies that some of the classes provide service to other classes, the server classes, and others require some other classes to perform their task, the client classes. In a lot of cases, a server class can also be a client to another server class.

Inheritance

    The root of the inheritance hierarchy of the Beach & Parking case study starts at Java's Applet class contained in java.Applet package. To simplify the discussion, the parent classes of Applet are not mentioned here. The same approach is also applied to other classes that the authors feel irrelevant to the case study. The BeachMap applet, which displays a coastal area in New South Wales, inherits from the Applet class. The BeachInfo shares the similar structure, except for some interfaces that will be looked at in more detail later. The BeachParking and the BeachStatistic applets does not inherit directly from Applet rather they inherit from Chart class and Spreadsheet respectively. Both of the latter classes inherits from Applet. As mentioned in the first paragraph, inheritance represents `is - a' relationship, this is true for direct or indirect inheritance. That means BeachParking or BeachStatistic can be used where an Applet object is used or required.

    The CustomImageCanvas of BeachMap is a decendant of ImageCanvas class. ImageCanvas inherits from Canvas to perform custom painting on the canvas area. This Canvas class is not shown in the diagram because it is not considered as one of the critical parts of the application.

    The next important aspect of the inheritance hierarchy particularly in relation to Java programming language is the notion of intefaces. Since Java does not support multiple inheritance, the concept of interface is introduced in Java to replace it. It is quite often to find that a class needs to inherit the charateristics of two different classes. Consider the following example, a class Oven may inherits from class Heater, that enables it to produce heat to cook the food, and class Timer, so it knows how long to cook the food for.

    Multiple inheritance sometime can create confusion even in a simple program. To illustrate the matter, consider the following diagram:

Multiple Inheritance

As shown in the diagram, class A is the ancestor of B and C, class D further extends the hierarchy by multiply-inheriting from B and C. Because of this arrangement, now D has two copies of class A. This is called repeated inheritance. Unless the nature of the inheritance is a virtual inheritance, which only produces only one copy of A, at some point in time the programmer has to choose which copy of class  A to use.

If B and C use same name for the a function, D has to be specific about which version ot the method to call. Java avoids the possiblility of confusion resulting from multiple inheritance. However, there must be a, way for a class to inherit the characteristics from more than one class. This is where interface comes and plays its role.

An interface only defines the signature of methods that must be defined by the class that implements the interface, no data variable is defined inside the interface. Java allows a class to implement multiple interfaces, the class can be referred to as it were that interface. Take the following code from the BeachMap applet as an example. The BeachMap applet implements two interfaces MouseListener and MouseMotionListener to enable it to define the appropriate methods in the interfaces to perform the desired operation when a mouse button is clicked on the canvas where the map is painted on or moved over one of the valid beaches.

    class BeachMap extends Applet implements MouseListener, MouseMotionListener{ ... }

Then to enable it to receive the mouse event, the canvas object registers the applet via a call to addMouseListener() method and addMouseMotionListener(). These methods take a MouseListener type object and a MouseMotionListener object respectively. As will be shown shortly, the BeachMap applet is passed as the parameter for both functions. This means that implementing an interface is similar to inheriting from a class.

    ...

    canvas.addMouseListener(this);

    canvas.addMouseMotionListener(this);

    ...

Having explained what interface is about, it is time to take a look at the interfaces hierarchy of the Beach and Parking case study. There are only two applets that interacts with the user, that is the BeachMap and  BeachInfo applets. As mentioned before, BeachMap implements two interfaces (see above) while BeachInfo implements ActionListener, MouseListener, and Runnable. For further information on BeachInfo and these interfaces, go to individual applet description.


Funded by Committee of University Teaching And Staff Development (CUTSD) through DEETYA, 1998