Using Interfaces for Code Reuse

public class DataSet // Modified for BankAccount objects
{
   . . .
   public void add(
BankAccount x)
   {
      sum = sum + x.
getBalance();
      if (count == 0 || maximum.
getBalance() < x.getBalance())
          maximum = x;
      count++;
   }

   public
BankAccount getMaximum()
   {
      return maximum;
   }
  
   private double sum;
   private
BankAccount maximum;
   private int count;
}

Or suppose we wanted to find the coin with the highest value among a set of coins. We would need to modify the DataSet class again:

public class DataSet // Modified for Coin objects
{
   . . .
   public void add(
Coin x)
   {
      sum = sum + x.
getValue();
      if (count == 0 || maximum.
getValue() < x.getValue())
          maximum = x;
      count++;
   }

   public Coin getMaximum()
   {
      return maximum;
   }

   private double sum;
   private
Coin maximum;
   private int count;
}

  sum = sum + x.getMeasure();

  if (count == 0 || maximum.getMeasure() < x.getMeasure())
    maximum = x;

  count++;

x should refer to any class that has a getMeasure method

  public interface Measurable

  {

     double getMeasure();

  }

Interfaces vs. Classes

An interface type is similar to a class, but there are several important differences:

Generic DataSet for Measurable Objects

public class DataSet
{
   . . .
   public void add(
Measurable x)
   {
      sum = sum + x.
getMeasure();
      if (count == 0 || maximum.
getMeasure() < x.getMeasure())
         maximum = x;
      count++;
   }

   public
Measurable getMaximum()
   {
      return maximum;
   }

   private double sum;
   private Measurable maximum;
   private int count;
}

Implementing an Interface Type

Use implements keyword to indicate that a class implements an interface type

public class BankAccount implements Measurable
{
  public double getMeasure()
  {
     return balance;
  }
 
// Additional methods and fields
}

A class can implement more than one interface type

public class Coin implements Measurable
{
  public double getMeasure()
  {
     return value;
  }
 
// Additional methods and fields
}

UML Diagram of DataSet and Related Classes

Interfaces are tagged with a "stereotype" indicator Ğinterfaceğ

A dotted arrow with a triangular tip denotes the "is-a" relationship between a class and an interface

A dotted line with an open v-shaped arrow tip denotes the "uses" relationship or dependency

Click here for Example of Measurable Interface.

Suppose you want to use the DataSet class to find the Country object with the largest population. What condition must the Country class fulfill?

   Answer: It must implement the Measurable interface, and its
  
getMeasure method must return the population.


Why can't the add method of the DataSet class have a parameter of type Object?

   Answer: The Object class doesn't have a getMeasure method,
   and the
add method invokes the getMeasure method.