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!

1

CMSC 132:

Object-Oriented Programming II

Java Inner Classes

Department of Computer Science

University of Maryland, College Park

2

Overview

Kinds of classes

Inner Nested

Motivating example

MyIterator design

Inner classes

Anonymous inner classes

Nested classes

Using inner classes in GUIs

3

Kinds of Classes

Top level classes

Declared inside package Visible throughout package, perhaps further Normally declared in their own file Public classes must be defined in their own file Not required for other classes

Inner and nested classes

Declared inside class (or method) Visible normally only to outer class Can have wider visibility

4

Kinds of Inner / Nested Classes

Inner class

Anonymous inner class

Nested class

Examples

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

7

Motivating Example

MyList

public class MyList { private Object [ ] a; private int size; }

Want to make MyList implement Iterable

Skipping generic types at the moment Need to be able to return an Iterator

8

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++]; } … }

9

MyIterator Design

Problems

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

Solution

Define MyIterator as inner class for MyList

10

MyIterator Design

Code

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

13

Inner Class Example

Class referencing syntax

OuterClass.InnerClass

Example

OC oc = new OC(); OC.IC ic; // name of inner class // ic = new OC.IC() doesn’t work! ic = oc.new IC(); // instantiates inner class // ic now will "know about" oc, but not vice versa

ic.getSum() yields 6 // can access private x in oc!

14

Method Resolution

Resolving method call on unspecified object

  1. See if method can be resolved on inner object
  2. If not, see if method can be resolved on corresponding instance of outer object
  3. If nested multiple levels, keep on looking

15

Creating/Referring to Inner Classes

Assume class A defines an inner class B

Inside instance methods of A, just use B as the

type of references to the inner class and use

new B(…) to create instances

Newly created B object associated with A object referenced by this

Outside of A, use A.B to name the inner class

If you need to create an instance of B

associated with a specific A object a, outside of

an instance method on a

Use a.new B() It is very rare for you to need to do this

16

Instantiating Inner Class

Common gimmick

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

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();

19

Syntax for Anonymous Inner Classes

Use

new Foo() {

public int one() { return 1; }

public int add(int x, int y) { return x + y; }

To define an anonymous inner class that:

Extends class Foo Defines methods one and add

20

Anonymous Inner Class

Properties

Inner class without name Instance of class returned by method

Syntax

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

21

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();

22

MyList Without Anonymous Inner Class

Code

public class MyList implements Iterable { private Object [ ] a; private int size; public Iterator iterator() { return new MyIterator(); } public class MyIterator implements Iterator { private int pos = 0; public boolean hasNext() { return pos < size; } public Object next() { return a[pos++]; } } }

25

Nested Classes

An instance of a nested class does not contain

an implicit reference to an instance of the outer

class

Still defined within outer class, has access to

all the private fields

Use if inner object might be associated with

different outer objects, or survive longer than

the outer object

Or just don’t want the overhead of the extra pointer in each instance of the inner object

26

Using Inner Classes in GUIs

javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndDisplayGUI(); } });

button.addActionListener (new ActionListener() {

public void actionPerformed (ActionEvent evt) { System.out.println(“Button pushed”); } });

27

Summary of Inner / Nested Classes

Inner class

Defined inside another class Each instance of an inner class is transparently associated with an instance of the outer class Method invocations can be transparently redirected to outer instance

Anonymous inner class

Unnamed inner class

Nested class

Defined inside another class Has access to private members of enclosing class Otherwise just a normal class