Using Interfaces in Java


In this Magercise, a new interface, Client, is used to allow a Company to set up contracts with Client's that may be individual Persons or other Companies.

The Client interface has three methods, one to get the name of the Client, another to get the amount of the contract, and a third to get a description of the contract. An examination of the starting model reveals that the NamedEntity class implements some name behavior. In order to maximize interface flexibility, a parent interface of Client is created as well. This interface, NamedInterface, will have a getName member. Client will extend this interface, and NamedEntity will implement it.

It is appropriate to leave NamedEntity as an abstract class because it implements useful behavior, the storing of names.

Interfaces are discussed in detail in the course notes.

Work Location

Perform all work for this magercise in VisualAge project MageLang Magercises, package magercises.client.

If this project does not appear in your Workspace add it from the repository (if it exists there) or create a new project using this name.

Tasks

Perform the following tasks:

  1. Create the interface, NamedInterface. It has one method, getName.

  2. Modify the abstract class NamedEntity so that it implements NamedInterface.

  3. Create the Client interface. It extends the NamedInterface with two new methods, contractAmount and contractDescription. The first method has no arguments and returns an int. The second also has no arguments but returns a String.

  4. Modify the Person class to implement the Client interface. The contractAmount method should return a constant value of 100. The contractDescription method should return a String with the name of the person and the amount of the contract.

  5. Modify the Company class to implement the Client interface in the same way, but this time return 200 for the value of the contract.

The task numbers above are linked to the step-by-step help page. Also available is a complete solution to the problem, and expected behavior, to demonstrate it.

Copyright © 1996-1997 MageLang Institute. All Rights Reserved.