Interfaces

9.5 - 9.6

Inner Class

Inner class at the class level

Inner class in a method

Inner class example

Creating a Comparator as an inner class

Inner class example (cont.)

Anonymous Objects

Anonymous Inner Classes

8/29 Lecture 2 Question 1

Let's make Item an inner class of ArrayListAddressBook (from the lab):

public class ArrayListAddressBook 
{
   private class Item 
   {
      String name;
      String key;
      String value;
   }
   ...
}

Which of the following is true?

  1. All methods of the ArrayListAddressBook class can construct and access Item objects
  2. Any code outside the ArrayListAddressBook class refers to the inner class as ArrayListAddressBook.Item
  3. The Item class is poorly designed because the instance variables of Item should have been declared as private
  4. The Item class should have been added to the AddressBook interface instead of the ArrayListAddressBook class

Mock Objects

Mock Objects

8/29 Lecture 2 Question 2

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

  1. In order to make use of already-written methods in Java classes such as Arrays and Collections
  2. In order to minimize the amount of code that needs to be updated when the mock class is replaced with the real class
  3. In order to allow two programmers to work independently on each class
  4. In order to reuse the address book class in other projects

Another interface example

Worked Example 9.1