Big Java 4

Chapter 9 – Interfaces and Polymorphism

Chapter Goals

Using Interfaces for Algorithm Reuse

Using Interfaces for Algorithm Reuse

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
{
   private double sum;
   private Coin maximum;
   private int count;
   . . .
   public void add(Coin x)
   {
      sum = sum + x.getValue();
      if (count == 0 || maximum.getValue() < x.getValue())
         maximum = x;
      count++;
   }

   public Coin getMaximum()
   {
      return maximum;
   }
}

Using Interfaces for Algorithm Reuse

Syntax 9.1 Declaring an Interface

Interfaces vs. Classes

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

DataSet for Measurable Objects

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

   public Measurable getMaximum()
   {
      return maximum;
   }
}

Implementing an Interface Type

Code Reuse

Syntax 9.2 Implementing an Interface

UML Diagram of DataSet and Related Classes

ch09/measure1/DataSetTester.java

Your browser does not support the <object> tag. Program Run:

Self Check 9.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?

Self Check 9.2

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

Converting Between Class and Interface Types

Variables of Class and Interface Types

Variables of Class and Interface Types

Casts

Self Check 9.3

Can you use a cast (BankAccount) meas to convert a Measurable variable meas to a BankAccount reference?

Self Check 9.4

If both BankAccount and Coin implement the Measurable interface, can a Coin reference be converted to a BankAccount reference?

Polymorphism

Interface Reference

Interface Reference

Polymorphism

Animation 9.1: Polymorphism

Link to Flash animation

Self Check 9.5

Why is it impossible to construct a Measurable object?

Self Check 9.6

Why can you nevertheless declare a variable whose type is Measurable?

Self Check 9.7

What does this code fragment print? Why is this an example of polymorphism?
DataSet data = new DataSet(); 
data.add(new BankAccount(1000)); 
data.add(new Coin(0.1, "dime")); 
System.out.println(data.getAverage());

Using Interfaces for Callbacks

Using Interfaces for Callbacks

Using Interfaces for Callbacks

UML Diagram of Measurer Interface and Related Classes

Note that the Rectangle class is decoupled from the Measurer interface

ch09/measure2/Measurer.java

Your browser does not support the <object> tag.

ch09/measure2/RectangleMeasurer.java

Your browser does not support the <object> tag.

ch09/measure2/DataSet.java

Your browser does not support the <object> tag.

ch09/measure2/DataSetTester2.java

Your browser does not support the <object> tag. Program Run:
Average area: 625
Expected: 625
Maximum area rectangle: java.awt.Rectangle[x=10,y=20,width=30,height=40]
Expected: java.awt.Rectangle[x=10,y=20,width=30,height=40]

Self Check 9.8

Suppose you want to use the DataSet class of Section 9.1 to find the longest String from a set of inputs. Why can't this work?

Self Check 9.9

How can you use the DataSet class of this section to find the longest String from a set of inputs?

Self Check 9.10

Why does the measure method of the Measurer interface have one more parameter than the getMeasure method of the Measurable interface?

Inner Classes

Inner Classes

ch09/measure3/DataSetTester3.java

Your browser does not support the <object> tag.

Self Check 9.11

Why would you use an inner class instead of a regular class?

Self Check 9.12

How many class files are produced when you compile the DataSetTester3 program?

Operating Systems

Mock Objects

Mock Objects

Mock Objects

Self Check 9.13

Why is it necessary that the real class and the mock class implement the same interface type?

Self Check 9.14

Why is the technique of mock objects particularly effective when the GradeBook and GradingProgram class are developed by two programmers?

Events, Event Sources, and Event Listeners

Events, Event Sources, and Event Listeners

Events, Event Sources, and Event Listeners

ch09/button1/ClickListener.java

Your browser does not support the <object> tag.

ch09/button1/ButtonViewer.java

Your browser does not support the <object> tag.

Self Check 9.15

Which objects are the event source and the event listener in the ButtonViewer program?

Self Check 9.16

Why is it legal to assign a ClickListener object to a variable of type ActionListener?

Using Inner Classes for Listeners

Using Inner Classes for Listeners

Using Inner Classes for Listeners

ch09/button2/InvestmentViewer1.java

Your browser does not support the <object> tag. Program Run:

Self Check 9.17

Why would an inner class method want to access a variable from a surrounding scope?

Self Check 9.18

If an inner class accesses a local variable from a surrounding scope, what special rule applies?

Building Applications with Buttons

Building Applications with Buttons

ch09/button3/InvestmentViewer2.java

Your browser does not support the <object> tag.

Self Check 9.19

How do you place the "balance: . . ." message to the left of the "Add Interest" button?

Self Check 9.20

Why was it not necessary to declare the button variable as final?

Processing Timer Events

ch09/timer/RectangleComponent.java

Displays a rectangle that can be moved

The repaint method causes a component to repaint itself. Call this method whenever you modify the shapes that the paintComponent method draws

Your browser does not support the <object> tag.

ch09/timer/RectangleMover.java

Your browser does not support the <object> tag.

Self Check 9.21

Why does a timer require a listener object?

Self Check 9.22

What would happen if you omitted the call to repaint in the moveBy method?

Mouse Events

Mouse Events

ch09/mouse/RectangleComponent.java

Your browser does not support the <object> tag.

Mouse Events

RectangleComponentViewer Program Run

ch09/mouse/RectangleComponentViewer.java

Your browser does not support the <object> tag.

Self Check 9.23

Why was the moveBy method in the RectangleComponent replaced with a moveTo method?

Self Check 9.24

Why must the MousePressListener class supply five methods?