








Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!









1
2
Inner Nested
MyIterator design
3
Kinds of 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
Declared inside class (or method) Visible normally only to outer class Can have wider visibility
4
Kinds of Inner / Nested Classes
public class MyOuterClass { public class MyInnerClass { … } static public class MyNestedClass { … } Iterator iterator( ) { return new Iterator( ) { … } } }
7
Motivating Example
public class MyList { private Object [ ] a; private int size; }
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
Need to maintain reference to MyList Need to access private data in MyList
Define MyIterator as inner class for MyList
10
MyIterator Design
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
OuterClass.InnerClass
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
15
Creating/Referring to Inner Classes
Newly created B object associated with A object referenced by this
Use a.new B() It is very rare for you to need to do this
16
Instantiating Inner Class
Outer class method returns instance of inner class Used by Java Collections Library for Iterators
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
Extends class Foo Defines methods one and add
20
Anonymous Inner Class
Inner class without name Instance of class returned by method
new ReturnType() { // unnamed inner class body of class… // implementing ReturnType } ;
21
Anonymous Inner Class
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
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
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
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
Unnamed inner class
Defined inside another class Has access to private members of enclosing class Otherwise just a normal class