topic 6 inner classes inner classes
play

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


  1. 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 But.... you can also declare a class inside another class anonymous classes Why would you want to? this lecture: Java mechanisms ● Common reason #1: sub-objects within objects later: motivation & typical uses ● Common reason #2: group related classes together Readings: from online Java Tutorial (link on web page) CISC 124, winter 2014, Set 6: Inner Classes 1 CISC 124, winter 2014, Set 6: Inner Classes 2 Example: Linked List ListCell as Inner Class public class LinkedList { doubly-linked list: two classes LinkedList methods ListCell : contains data, pointers to next & previous cells // inner class LinkedList : pointers to head & tail may create and use ListCell objects. private class ListCell { ListCell is helper class int value; Only used for LinkedList ListCell next; Outside users of ListCell previous; LinkedList can't see Picture cells as being "inside" list: } // end ListCell ListCell class – LinkedList implementation detail. private ListCell head; ListCell ListCell ListCell private ListCell tail; head 1 2 3 tail ...plus lots of methods... } // end LinkedList CISC 124, winter 2014, Set 6: Inner Classes 3 CISC 124, winter 2014, Set 6: Inner Classes 4

  2. Limits on Inner Class Use More Capabilities of Inner Classes public class OuterClass { public class SlideDemo { .... private int a; public InnerClass subObject; public InnerClass subObject; public class InnerClass { public class InnerClass { public int b; public int b; public int sum() { .... reference to a in enclosing object return a + b; } // end class InnerClass } // end sum } // end class InnerClass } // end class OuterClass // constructor public SlideDemo(int aval, int bval) { a = aval; an outside user can reference the inner object: subObject = new InnerClass(); OuterClass x = new OuterClass(); subObject.b = bval; } // end constructor int y = x.subObject.b; public static void main(String args[]) { an outside user may not create an instance of InnerClass : SlideDemo tester = new SlideDemo(7, 5); System.out.println(tester.subObject.sum()); InnerClass y = tester.a = 4; new OuterClass.InnerClass(); // illegal tester.subObject.b = 3; System.out.println(tester.subObject.sum()); } // end main InnerClass objects may only exist inside OuterClass objects } // end class SlideDemo CISC 124, winter 2014, Set 6: Inner Classes 5 CISC 124, winter 2014, Set 6: Inner Classes 6 Implementation Details Static Nested Classes public class OuterClass { Our mental picture of the objects: private int a; public InnerClass subObject; inner class with "static" a: 13 subObject is "inside" public static class InnerClass { called a "nested class" subObject b: 7 public int b; enclosing object public int sum() { reference to members of enclosing return a + b; } // end sum class is illegal } // end class InnerClass How Java really does it: pointer from "inner" object to // main for testing "outer" object is gone public static void main(String args[]) { a: 13 OuterClass tester = new OuterClass(); tester.a = 7; subObject b: 7 tester.subObject.b = 5; System.out.println(tester.subObject.sum()); pointer from inner object } // end main to enclosing object } // end class OuterClass CISC 124, winter 2014, Set 6: Inner Classes 7 CISC 124, winter 2014, Set 6: Inner Classes 8

  3. Why Use Static Nested Classes? Anonymous Classes: Motivation Define one class inside another to reflect relationship between Suppose we want a slight modification of another class. classes. One common use is exceptions (next topic) Example: a stack that prints debug output automatically. One way: extend a stack implementation public class MyClass { class DebugStack extends ArrayStack { // data and methods for MyClass public void push(int i) { super.push(i); // MyClass may raise exceptions System.out.println("just pushed " + i); public static class MyException { } .. public int pop() { } int result = super.pop(); } // end MyClass System.out.println("just popped " + result); return result; } A MyException object is not a part of a MyClass object } Will be created by MyClass methods (instance or static methods) CISC 124, winter 2014, Set 6: Inner Classes 9 CISC 124, winter 2014, Set 6: Inner Classes 10 Anonymous Class Anonymous Classes & Interfaces What if we only want to create one instance of DebugStack ? You can use an anonymous class to create an object that Can use an anonymous class: implements an interface Stack myStack = new ArrayStack() { public void push(int i) { Stack myStack = new Stack() { super.push(i); public void push(int i) {...} System.out.println("just pushed " + i); public int pop() {...} } public int pop() { boolean isEmpty() {...} 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 CISC 124, winter 2014, Set 6: Inner Classes 11 CISC 124, winter 2014, Set 6: Inner Classes 12

  4. What's Important Here? • 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! Examples of all three will come up during this course. CISC 124, winter 2014, Set 6: Inner Classes 13

Recommend


More recommend