Big Java 4

Chapter 9 – Interfaces and Polymorphism

Chapter Goals

mixer to show interfaces

Using Interfaces for Algorithm Reuse

Defining an Interface Type

Defining an Interface Type

Syntax 10.1 Declaring an Interface

syntax of an interface

Defining an Interface Type

Defining an Interface Type

Implementing an Interface Type

Implementing an Interface Type

Implementing an Interface Type

Syntax 10.2 Implementing an Interface

implementing interface

section_1/Data.java

Your browser does not support this feature

section_1/MeasurableTester.java

Your browser does not support this feature Program Run:

Comparing Interfaces and Inheritance

Question 1 May 8

Suppose you want to use the average method to find the average salary of an array of Employee objects. What condition must the Employee class fulfill?
  1. It must extend Measurable and its getMeasure method must return the salary
  2. It must implement the Measurable interface, and its getMeasure method must return the salary.
  3. It must have an average method.
  4. Something else

Self Check 10.2

Why can’t the average method have a parameter variable of type Object[]?

Question 2 May 8

Can you use the average method to find the average length of String objects? Why or why not?
  1. Yes. You can use the average method to find the average of any array of objects
  2. Yes, but you would need to modify the String class to implement Measurable interface
  3. No. You cannot modify the String class to implement Measurable
  4. No. String does not extend Measurable

Self Check 10.4

What is wrong with this code?
Measurable meas = new Measurable();
System.out.println(meas.getMeasure());

Self Check 10.5

What is wrong with this code?
Measurable meas = new Country("Uruguay", 176220);
System.out.println(meas.getName());

Converting From Classes to Interfaces

Variables of Class and Interface Types

Casting from Interfaces to Classes

Casting from Interfaces to Classes

Question 1 May 13

Can you use a cast (Country) meas to convert a Measurable variable meas to a Country reference? (Country implements Measurable)
  1. Yes, always
  2. No, never
  3. Yes if meas actually refers to a Country object.

Self Check 10.7

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

Question 2 May 13

Which code below will construct a Measurable object?

  1. Measurable thing;
  2. Measurable thing = new Measurable();
  3. Measurable thing = new Rectangle();
  4. You can not create a Measurable object.

Self Check 10.9

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

Self Check 10.10

What does this code fragment print? Why is this an example of polymorphism?
Measurable[] data = { new BankAccount(10000), new Country("Belgium", 30510) };
System.out.println(average(data));

The Comparable Interface

The Comparable Interface

The Comparable Interface

Self Check 10.11

How can you sort an array of Country objects by increasing area?

Self Check 10.12

Can you use the Arrays.sort method to sort an array of String objects? Check the API documentation for the String class.

Self Check 10.13

Can you use the Arrays.sort method to sort an array of Rectangle objects? Check the API documentation for the Rectangle class.

Self Check 10.14

Write a method max that finds the larger of any two Comparable objects

Self Check 10.15

Write a call to the method of Self Check 14 that computes the larger of two bank accounts, then prints its balance.

Using Interfaces for Callbacks

Using Interfaces for Callbacks

Using Interfaces for Callbacks

Using Interfaces for Callbacks

Using Interfaces for Callbacks

section_4/Measurer.java

Your browser does not support this feature

section_4/AreaMeasurer.java

Your browser does not support this feature

section_4/Data.java

Your browser does not support this feature

section_4/MeasurerTester.java

Your browser does not support this feature Program Run:

Self Check 10.16

Suppose you want to use the average method of Section 10.1 to find the average length of String objects. Why can’t this work?

Self Check 10.17

How can you use the average class of this section to find the average length of String objects?

Self Check 10.18

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

Self Check 10.19

Write a method max with three arguments that finds the larger of any two objects, using a Measurer to compare them.

Self Check 10.20

Write a call to the method of Self Check 19 that computes the larger of two rectangles, then prints its width and height

Inner Classes

Inner Classes

Self Check 10.21

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

Self Check 10.22

When would you place an inner class inside a class but outside any methods?

Self Check 10.23

How many class files are produced when you compile the MeasurerTester program from this section?

Mock Objects

Mock Objects

Mock Objects

Mock Objects

Self Check 10.24

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

Self Check 10.25

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

Event Handling

Event Handling

Events Handling

section_7_1/ClickListener.java

Your browser does not support this feature

Event Handling - Listening to Events

section_7_1/ButtonViewer.java

Your browser does not support the <object> tag.

Event Handling

Just a button

Whenever a button is pressed, the actionPerformed method is called on all listeners.

Specify button click actions through classes that implement the ActionListener interface.

Using Inner Classes for Listeners

Using Inner Classes for Listeners

section_7_2/InvestmentViewer1.java

Your browser does not support this feature Program Run:

Self Check 10_26

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

Self Check 10_27

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

Self Check 10.28

When do you call the actionPerformed method?

Self Check 10.29

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

Self Check 10.30

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

Building Applications with Buttons

Building Applications with Buttons

section_8/InvestmentViewer2.java

Your browser does not support the <object> tag.

Self Check 10.31

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

Self Check 10.32

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

Processing Timer Events

section_9/RectangleComponent.java

Displays a rectangle that moves

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.

section_9/RectangleFrame.java

Your browser does not support the <object> tag.

section_9/RectangleViewer.java

Your browser does not support the <object> tag.

Self Check 10.33

Why does a timer require a listener object?

Self Check 10.34

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

Mouse Events

Mouse Events

section_10/RectangleComponent2.java

First add a moveRectangle method to RectangleComponent

Your browser does not support the <object> tag.

Mouse Events

RectangleViewer2 Program Run

GUI for moving rectangle

section_10/RectangleFrame2.java

Your browser does not support the <object> tag.

section_10/RectangleViewer2.java

Your browser does not support the <object> tag.

Self Check 10.35

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

Self Check 10.36

Why must the MousePressListener class supply five methods?