Chapter 11

Interfaces and Polymorphism


Chapter Goals

Using Interfaces for Code Reuse

Using Interfaces for Code Reuse

Using Interfaces for Code Reuse

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

UML Diagram of DataSet and Related Classes

Syntax 11.1: Defining an Interface

 
public interface InterfaceName
{
   // method signatures
}

Example:

 
public interface Measurable
{
   double getMeasure();
}

Purpose:

To define an interface and its method signatures. The methods are automatically public.

Syntax 11.2: Implementing an Interface

 
public class ClassName
   implements InterfaceName, InterfaceName, ...
{
      // methods
      // instance variables
}

Example:

 
public class BankAccount implements Measurable
{
   // Other BankAccount methods
   public double getMeasure()
   {
      // Method implementation
   }
 }

Purpose:

To define a new class that implements the methods of an interface

File DataSetTester.java


Output
   Average balance = 4000.0
   Highest balance = 10000.0
   Average coin value = 0.13333333333333333
   Highest coin value = 0.25

Self Check

  1. Suppose you want to use the DataSet class to find the Country object with the largest population. What condition must the Country class fulfill?
  2. Why can't the add method of the DataSet class have a parameter of type Object?

Answers

  1. It must implement the Measurable interface, and its getMeasure method must return the population
  2. The Object class doesn't have a getMeasure method, and the add method invokes the getMeasure method

Converting Between Class and Interface Types

Casts

Self Check

  1. Can you use a cast (BankAccount) x to convert a Measurable variable x to a BankAccount reference?
  2. If both BankAccount and Coin implement the Measurable interface, can a Coin reference be converted to a BankAccount reference?

Answers

  1. Only if x actually refers to a BankAccount object.
  2. No–a Coin reference can be converted to a Measurable reference, but if you attempt to cast that reference to a BankAccount, an exception occurs.

Polymorphism

Polymorphism

Self Check

  1. Why is it impossible to construct a Measurable object?
  2. Why can you nevertheless declare a variable whose type is Measurable?
  3. What do overloading and polymorphism have in common? Where do they differ?

Answers

  1. Measurable is an interface. Interfaces have no fields and no method implementations.
  2. That variable never refers to a Measurable object. It refers to an object of some class–a class that implements the Measurable interface.
  3. Both describe a situation where one method name can denote multiple methods. However, overloading is resolved early by the compiler, by looking at the types of the parameter variables. Polymorphism is resolved late, by looking at the type of the implicit parameter object just before making the call.

Using Interfaces for Callbacks

Using Interfaces for Callbacks

Using Interfaces for Callbacks

UML Diagram of Measurer Interface and Related Classes

File DataSet.java

File DataSetTester2.java

File Measurer.java

File RectangleMeasurer.java


Output
   Average area = 616.6666666666666
   Maximum area rectangle = java.awt.Rectangle[x=10,y=20,width=30,height=40]

Self Check

  1. Suppose you want to use the DataSet class of Section 11.1 to find the longest String from a set of inputs. Why can't this work?
  2. How can you use the DataSet class of this section to find the longest String from a set of inputs?
  3. Why does the measure method of the Measurer interface have one more parameter than the getMeasure method of the Measurable interface?

Answers

  1. The String class doesn't implement the Measurable interface.
  2. Implement a class StringMeasurer that implements the Measurer interface.
  3. A measurer measures an object, whereas getMeasure measures "itself", that is, the implicit parameter.

Inner Classes

Syntax 11.3: Inner Classes

  Declared inside a method
class OuterClassName
{
  method signature
  {
     . . .
     class InnerClassName
     {
        // methods
        // fields
     }
     . . .
  }
  . . .
}
  Declared inside the class
class OuterClassName
{
   // methods
   // fields
   accessSpecifier class InnerClassName
   {
      // methods
      // fields
   }
   . . .
}

Example:

 
public class Tester
{
   public static void main(String[] args)
   {
      class RectangleMeasurer implements Measurer
      {
         . . .
      }
      . . .
   }
}

Purpose:

To define an inner class whose scope is restricted to a single method or the methods of a single class

File DataSetTester3.java

Self Check

  1. Why would you use an inner class instead of a regular class?
  2. How many class files are produced when you compile the DataSetTester3 program?

Answers

  1. Inner classes are convenient for insignificant classes. Also, their methods can access variables and fields from the surrounding scope.
  2. Four: one for the outer class, one for the inner class, and two for the DataSet and Measurer classes.

Processing Timer Events

Example: Countdown

File TimerTester.java

Self Check

  1. Why does a timer require a listener object?
  2. How many times is the actionPerformed method called in the preceding program?

Answers

  1. The timer needs to call some method whenever the time interval expires. It calls the actionPerformed method of the listener object.
  2. It depends. The method is called once per second. The first eleven times, it prints a message. The remaining times, it exits silently. The timer is only terminated when the user quits the program.

Accessing Surrounding Variables

Accessing Surrounding Variables

File TimerTester2.java

Output

java.awt.Rectangle[x=6,y=11,width=20,height=30]
java.awt.Rectangle[x=7,y=12,width=20,height=30]
java.awt.Rectangle[x=8,y=13,width=20,height=30]
. . .
java.awt.Rectangle[x=28,y=33,width=20,height=30]
java.awt.Rectangle[x=29,y=34,width=20,height=30]
Last box position: java.awt.Rectangle[x=29,y=34,width=20,height=30]

Self Check

  1. Why would an inner class method want to access a variable from a surrounding scope?
  2. If an inner class accesses a local variable from a surrounding scope, what special rule applies?

Answers

  1. Direct access is simpler than the alternative–passing the variable as a parameter to a constructor or method.
  2. The local variable must be declared as final.

Operating Systems