07InnerClasses

07InnerClasses - CMSC 132: Object-Oriented Programming II...

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

View Full Document Right Arrow Icon
1 CMSC 132: Object-Oriented Programming II Java Inner Classes Department of Computer Science University of Maryland, College Park
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 Java 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) Normally used only in outer (enclosing) class Can have wider visibility
Background image of page 2
3 Inner / Nested Classes Inner class Nested class public class MyOuterClass { public class MyInnerClass { … } static public class MyNestedClass { … } } }
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 Inner Classes Description Class defined in scope of another class May be named or anonymous Useful property Outer & inner class can directly access each other’s fields & methods (even if private)
Background image of page 4
5 Inner Class Link To Outer Class Inner class instance Has association to an instance of outer class Must be instantiated with an enclosing instance Is tied to outer class object at moment of creation (can not be changed) MyList MyList MyIterator MyIterator MyIterator
Background image of page 5

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

View Full DocumentRight Arrow Icon
Syntax Class Outer { class Inner { } Inner x = new Inner(); } Outer out = new Outer(); Outer.Inner in = out.new Out.Inner();
Background image of page 6
7 Using Inner Class Inside Outer Class Code public class OC { // outer class private int x = 2; private class IC { // inner class private int y = 4; private int getSum() { return x + y; } } void bar( ) { IC z = new IC( ); // create new IC assoc. w/ this z .getSum( ); // treat z like normal object } }
Background image of page 7

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

View Full DocumentRight Arrow Icon
8 Inner Classes Example public class OuterClass { private int x; private class InnerClass { private int y; void foo( ) { x = 1; } // access private field } void bar( ) { InnerClass ic = new InnerClass( ); ic.y = 2; // access private field } }
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 27

07InnerClasses - CMSC 132: Object-Oriented Programming II...

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

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