15InnerClassCollections - CS108 Stanford Winter 2012...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Handout #15 Winter 2012 Young Inner/Nested Classes Collection Implementation Handout by Nick Parlante Inner and Nested Classes Suppose you have some "outer" object Inner and nested classes allow us to create a small, subsidiary object of the outer object. The inner or nested object is closely integrated with the outer object. Appropriate if need the inner object as a temporary, separate feature of the outer object (e.g. iterator) Appropriate if we need many little inner/nested objects to go with the one outer object (e.g. binary tree nodes) What Does a Function Pointer Accomplish? Suppose we have objects Alice and Bob Goal: Alice has code. Alice gives something to Bob that Bob can hold, and later on, it allows Bob to call Alice's code in some way. OOP: Alice gives Bob an object. The object responds to messages foo() and bar(). Bob can hold the object, and later on, call foo() and bar() on it. So A implements the code, gives something to B, where B can invoke the code later. Inner classes are widely used to solve this sort of function-pointer problem in Java. In Java 7, there are proposals for a "closure" feature, making a simpler syntax for this sort of thing. Inner Class An "inner" class is a class defined inside some other "outer" class. The inner class may or may not be exposed for use by clients. (See the iterator example below) Use an inner class when you need one or more separate objects (like an iterator object) where it makes sense for that object to be closely related to some outer object. e.g. A BinaryTree class might have an inner "Node" class that it uses internally to build the tree. The Node class is probably not exposed to clients -- it's just for internal use. The inner class operates like a sub-part of the outer class The inner class can have ivars, a ctor, etc. just like a regular class. With classes named "Outer" and "Inner", the full name of the Inner class is "Outer.Inner". Access style - The outer and inner classes can access each other's state, even if it is private . Stylistically, they are basically one implementation code base, so mixing access is ok, but we prefer receiver- relative coding for both the outer and inner classes where it makes sense. Inner class usually created in the context of an "owning" outer object. Normally, the "new" call to make the inner is done from the context of a method of the outer object. - Calling "new" just anywhere will not work to create an inner object, because of the need for an outer object. The very obscure syntax "outer.new Inner()" can create object of class Inner owned by an "outer" object. I mention this for completeness only. The standard way is to call "new Inner()" inside an outer class method to create new inner objects. The inner object automatically has a pointer to its outer object -- can access ivars of outer object
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/12/2012 for the course CS 108 taught by Professor Jimenez during the Winter '08 term at Stanford.

Page1 / 7

15InnerClassCollections - CS108 Stanford Winter 2012...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online