Docsity

Java Inner and Nested Classes: Understanding the Relationship with Outer Classes - Prof. N, Study notes of Computer Science

Partial preview of the text

Download Java Inner and Nested Classes: Understanding the Relationship with Outer Classes - Prof. N and more Study notes Computer Science in PDF only on Docsity!

CMSC 132:

Object-Oriented Programming II

Java Inner Classes

Department of Computer Science

University of Maryland, College Park

Overview

Classes

Top-level vs. inner & nested

Inner classes

Iterator example Used inside outer class

Anonymous inner classes

Syntax Uses for GUIs

Nested classes

Inner / Nested Classes

Inner class

Nested class

Anonymous inner class

Examples

public class MyOuterClass { public class MyInnerClass { … } static public class MyNestedClass { … } Iterator iterator( ) { return new Iterator( ) { … } } }

Inner Classes

Description

Class defined in scope of another class May be named or anonymous

Useful property

Outer & inner class can directly access each other’s fields & methods (even if private)

Inner Class Link To Outer Class

Inner class instance

Has association to an instance of outer class Must be instantiated with an enclosing instance Is tied to outer class object at moment of creation (can not be changed)

MyList MyList

MyIterator MyIterator MyIterator

Inner Classes

Useful for

Private helper classes Logical grouping of functionality Data hiding Linkage to outer class Inner class object tied to outer class object

Examples

Iterator for Java Collections ActionListener for Java GUI widgets

(Problematic) MyIterator Design

public class MyIterator implements Iterator { private MyList list; private int pos; MyIterator(MyList list) { this.list = list; pos = 0; } public boolean hasNext() { return pos < list.size; } public Object next() { return list.a[pos++]; } … }

MyIterator Design

Problems

Need to maintain reference to MyList Need to access private data in MyList

Solution

Define MyIterator as inner class for MyList Instance of MyIterator tied to instance of MyList MyIterator methods can access private MyList fields MyIterator objects can iterate through elements of MyList

Iterable Interface

Iterable interface defines the method

Iterator iterator()

Part of java.lang

Returns an iterator over a set of elements of

type T

Implementing this interface allows an object to

be the target of the enhanced for loop "foreach"

statement

Instantiating Inner Class

How to access instance of inner class?

Common gimmick

Outer class method returns instance of inner class Used by Java Collections Library for Iterators

Example code

public class MyList { public class IC implements Iterator { … } public Iterator iterator() { return new IC(); // creates instance of IC } } MyList m = new MyList(); Iterator it = m.iterator();

Using Inner Class Inside Outer Class

Code

public class OC { // outer class private int x = 2; private class IC { // inner class private int y = 4; private int getSum() { return x + y; } } void bar( ) { IC z = new IC( ); // create new IC assoc. w/ this z.getSum( ); // treat z like normal object } }

Accessing Outer Scope

Code

public class OC { // outer class int x = 2; private class IC { // inner class int x = 6; private void getX() { // inner class method int x = 8; System.out.println( x ); // prints 8 System.out.println( this.x ); // prints 6 System.out.println( OC.this.x ); // prints 2 } } }

Anonymous Inner Class

Description

Inner class without name Defined where you create an instance of it In the middle of a method Returns an instance of anonymous inner class Useful if the only thing you want to do with an inner class is create instances of it in one location

Syntax

new ReturnType( ) { // unnamed inner class body of class… // implementing ReturnType } ;

Anonymous Inner Class

Code

public class MyList { public Iterator iterator( ) { return new Iterator( ) { // unnamed inner class … // implementing Iterator } ; } } MyList m = new MyList( ); Iterator it = m.iterator( );