Topic 6: Inner Classes Inner Classes What's an inner class? A class - - PowerPoint PPT Presentation

topic 6 inner classes inner classes
SMART_READER_LITE
LIVE PREVIEW

Topic 6: Inner Classes Inner Classes What's an inner class? A class - - PowerPoint PPT Presentation

Topic 6: Inner Classes Inner Classes What's an inner class? A class defined inside another class First understanding of Java: Three kinds: all classes are "top level", usually one per file inner classes static nested classes


slide-1
SLIDE 1

1 CISC 124, winter 2014, Set 6: Inner Classes

Topic 6: Inner Classes

Readings: from online Java Tutorial (link on web page) What's an inner class? A class defined inside another class Three kinds:

inner classes static nested classes anonymous classes this lecture: Java mechanisms later: motivation & typical uses

2 CISC 124, winter 2014, Set 6: Inner Classes

First understanding of Java:

all classes are "top level", usually one per file

Inner Classes

But.... you can also declare a class inside another class Why would you want to?

  • Common reason #1: sub-objects within objects
  • Common reason #2: group related classes together

3 CISC 124, winter 2014, Set 6: Inner Classes

Example: Linked List

doubly-linked list: two classes ListCell: contains data, pointers to next & previous cells LinkedList: pointers to head & tail ListCell is helper class Only used for LinkedList

1 2 head

LinkedList

tail 3

ListCell ListCell ListCell

Picture cells as being "inside" list:

4 CISC 124, winter 2014, Set 6: Inner Classes

public class LinkedList { // inner class private class ListCell { int value; ListCell next; ListCell previous; } // end ListCell private ListCell head; private ListCell tail; ...plus lots of methods... } // end LinkedList

ListCell as Inner Class

LinkedList methods may create and use ListCell objects. Outside users of LinkedList can't see ListCell class – implementation detail.

slide-2
SLIDE 2

5 CISC 124, winter 2014, Set 6: Inner Classes

More Capabilities of Inner Classes

public class SlideDemo { private int a; public InnerClass subObject; public class InnerClass { public int b; public int sum() { return a + b; } // end sum } // end class InnerClass // constructor public SlideDemo(int aval, int bval) { a = aval; subObject = new InnerClass(); subObject.b = bval; } // end constructor public static void main(String args[]) { SlideDemo tester = new SlideDemo(7, 5); System.out.println(tester.subObject.sum()); tester.a = 4; tester.subObject.b = 3; System.out.println(tester.subObject.sum()); } // end main } // end class SlideDemo

reference to a in enclosing object

6 CISC 124, winter 2014, Set 6: Inner Classes

public class OuterClass { .... public InnerClass subObject; public class InnerClass { public int b; .... } // end class InnerClass } // end class OuterClass

Limits on Inner Class Use

an outside user can reference the inner object:

OuterClass x = new OuterClass(); int y = x.subObject.b;

an outside user may not create an instance of InnerClass:

InnerClass y = new OuterClass.InnerClass(); // illegal

InnerClass objects may only exist inside OuterClass objects

7 CISC 124, winter 2014, Set 6: Inner Classes

Our mental picture of the objects:

Implementation Details

a: 13 subObject b: 7

subObject is "inside" enclosing object

How Java really does it:

a: 13 subObject b: 7 pointer from inner object to enclosing object

8 CISC 124, winter 2014, Set 6: Inner Classes

public class OuterClass { private int a; public InnerClass subObject; public static class InnerClass { public int b; public int sum() { return a + b; } // end sum } // end class InnerClass // main for testing public static void main(String args[]) { OuterClass tester = new OuterClass(); tester.a = 7; tester.subObject.b = 5; System.out.println(tester.subObject.sum()); } // end main } // end class OuterClass

Static Nested Classes

reference to members of enclosing class is illegal pointer from "inner" object to "outer" object is gone inner class with "static" called a "nested class"

slide-3
SLIDE 3

9 CISC 124, winter 2014, Set 6: Inner Classes

Define one class inside another to reflect relationship between

classes.

One common use is exceptions (next topic)

Why Use Static Nested Classes?

public class MyClass { // data and methods for MyClass // MyClass may raise exceptions public static class MyException { .. } } // end MyClass

A MyException object is not a part of a MyClass object Will be created by MyClass methods (instance or static methods)

10 CISC 124, winter 2014, Set 6: Inner Classes

Suppose we want a slight modification of another class. Example: a stack that prints debug output automatically. One way: extend a stack implementation

Anonymous Classes: Motivation

class DebugStack extends ArrayStack { public void push(int i) { super.push(i); System.out.println("just pushed " + i); } public int pop() { int result = super.pop(); System.out.println("just popped " + result); return result; } }

11 CISC 124, winter 2014, Set 6: Inner Classes

What if we only want to create one instance of DebugStack? Can use an anonymous class:

Anonymous Class

Stack myStack = new ArrayStack() { public void push(int i) { super.push(i); System.out.println("just pushed " + i); } public int pop() { int result = super.pop(); System.out.println("just popped " + result); return result; } };

Note: anonymous classes are useful only for changing existing methods – not adding new ones

12 CISC 124, winter 2014, Set 6: Inner Classes

You can use an anonymous class to create an object that implements an interface

Anonymous Classes & Interfaces

Stack myStack = new Stack() { public void push(int i) {...} public int pop() {...} boolean isEmpty() {...} };

slide-4
SLIDE 4

13 CISC 124, winter 2014, Set 6: Inner Classes

  • understand the basic idea of an inner class
  • understand differences between the three kinds
  • don't sweat the syntax of anonymous classes – you will always

have examples to copy from!

What's Important Here?

Examples of all three will come up during this course.