- Forward


Nested Classes
An Introduction with Examples in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Motivation
Back SMYC Forward
  • One Common Situation:
    • One or more classes is tightly coupled to one other class and only used by that class (e.g., a Course class that has associated Comparator classes; a ClosedInterval class that has associated Iterator classes)
  • A Simple Improvement:
    • Allow classes to be nested (i.e., for one class to be defined inside of another)
Motivation
Back SMYC Forward
  • Another Common Situation:
    • A (often abstract) base class has derived classes that vary in similar and limited ways (e.g., an abstract Rectangle2D class that has two concrete derived classes, one that uses double values and one that uses float values)
  • A Simple Improvement:
    • Though historically this situation was dealt with using nested classes it is usually better to use a parameterized class
Nested Classes in UML
Back SMYC Forward

Containment

UML-nested-classes
Two Different Approaches
Back SMYC Forward
  • Static Nested Class:
    • Is associated with the class (not an instance) so can't directly refer to instance attributes/methods in its enclosing class (can only do so through an object reference)
  • Inner Class:
    • Is associated with an instance of its enclosing class (not the class itself) so has direct access to that object's attributes/methods
Static Nested Classes
Back SMYC Forward
  • Interaction With Instances of the Outer Class:
    • Identical to "top-level" classes
  • Primary Advantage Over "Top-Level" Classes:
    • Organizational convenience
  • Construction:
    • Must use the syntax OuterClassName.StaticNestedClassName
Static Nested Classes (cont.)
Back SMYC Forward
  • An Example from Another Lecture:
    • A Course class with "top-level" CreditComparator and IDComparator classes
  • Improving the Design:
    • Since the two Comparator classes will only ever be used with the Course class (i.e., they can't stand alone), it makes sense to make them static nested classes
Static Nested Classes (cont.)
Back SMYC Forward

The Improved Implementation

javaexamples/nested/Course.java
 
Static Nested Classes (cont.)
Back SMYC Forward

Using the Static Nested Classes

javaexamples/nested/CourseDriver.java
 
Inner Classes
Back SMYC Forward
  • Context:
    • Instances of an inner class exist within an instance of the outer class
  • Construction:
    • An instance of the outer class must be created before the instance of the inner class can be
  • An Implication/Restriction:
    • Inner classes can't declare any static members
Inner Classes (cont.)
Back SMYC Forward
  • Interaction With Instances of the Outer Class:
    • Has access to all of the members of the containing/enclosing object
  • Primary Advantages Over "Top-Level" Classes:
    • Organizational convenience
    • Access to private members
  • Construction:
    • The constructor is referred to using the syntax object.InnerClassName()
    • A factory method is often used
Inner Classes (cont.)
Back SMYC Forward
  • An Example:
    • A ClosedInterval class and associated IncreasingIterator class
  • Why Not A Static Nested Class?
    • An IncreasingIterator object is associated with a particular ClosedInterval object
Inner Classes (cont.)
Back SMYC Forward

An Implementation without an Inner Class

javaexamples/iterator/ClosedInterval.java
 
Inner Classes (cont.)
Back SMYC Forward

An Implementation without an Inner Class (cont.)

javaexamples/iterator/IncreasingIterator.java
 
Inner Classes (cont.)
Back SMYC Forward

An Implementation with an Inner Class

javaexamples/nested/ClosedInterval.java
 
Inner Classes (cont.)
Back SMYC Forward

Using the Inner Class

javaexamples/nested/IntervalDriver.java
 
Beyond the Scope of this Lecture
Back SMYC Forward
  • Local Classes:
    • Classes defined in a block (usually a method) and, hence, not "widely available"
  • Anonymous Classes:
    • Enable the declaration and instantation of a class at the same time
    • Useful in situations where a local class could be used, but only once
There's Always More to Learn
Back -