Blakes 21 Days Chapter 16 Document


Day 16, Using Inner Classes and Closures

Inner Classes

First Program - Listing 16.1

Listing 16.1 - The Full Text of ComicBox.java

The Explanation

Figure 16.1 - Using inner classes to collect comic books. - goes here

500

Anonymous Inner Classes

Second Program

LISTING 16.2 The Full Text of ProgressMonitor2.java

The Explanation

Figure 16.2 - Stopping a program from closing until its work is done. - goes here

508

Closures

Cursors

Third Program - Listing 16.3

LISTING 16.3 The Full Text of CursorMayhem.java

Figure 16.3 - Monitoring action events with an anonymous inner class. - goes here

512

The Explanation

Fourth Program - Listing 16.4

LISTING 16.4 The Full Text of ClosureMayhem.java

The Explanation



Summary

Q & A

Quiz - Questions

  1. Whatcan an inner class access that a separate helper class could not ?
    1. Anonymous inner classes
    2. private variables of another class
    3. Threads
  2. What makes a Java interface qualified to be called a functional interface ?
    1. The number of methods in that interface
    2. The arrow operator
    3. Any interface can be functional.
  3. What does an adapter class make easier ?
    1. The use of closures
    2. Arranging Swing user interface components
    3. Implementing an event listener

Answers

  1. B. An inner class can access the private variables and methods of its enclosing class.
  2. A. An interface that defines only one method is a functional interface.
  3. C. An adapter implements all the methods in an event listener interface so you can subclass the adapter and override only the method or methods that are useful.

Certification Practice

  1. What will be the output of this application ?
    1. true
    2. false
    3. Error
    4. The program will not compile.

Exercise