OOP in Java – Inner ClassesPackageScopeScope – ExampleInner ClassesSlide 6Motivating ExampleMyIterator DesignSlide 9Slide 10Slide 11Inner Classes ExampleSlide 13Accessing Outer ScopeInstantiating Inner ClassAnonymous Inner ClassSlide 17OOP in Java – Inner ClassesFawzi EmadChau-Wen TsengDepartment of Computer ScienceUniversity of Maryland, College ParkPackagePackageGroup of related classes under one nameHelps manage software complexityImportMake classes from package available for useJava API – java.* (core), javax.* (optional)Examplepackage edu.umd.cs; // name of packageimport java.util.Random; // import single classimport java.util.*; // all classes in package… // class definitionsScopeScopePart of program where a variable may be referencedDetermined by location of variable declarationTypesTop level classesDeclared inside packageVisible throughout packageNested classesDeclared inside class (or method)Visible only inside classScope – ExampleExamplepackage edu.umd.cs ;public class MyClass1 { public void MyMethod1 { ... } public void MyMethod2 { ... }}public class MyClass2 {}MethodMethodClassPackageClassScopesInner ClassesDescriptionClass defined in scope of another classMay be named or anonymousPropertyCan directly access all variables & methods of enclosing class (including private fields & methods)Inner ClassesUseful forLogical grouping of functionalityData hidingLinkage to outer classExamplepublic class OuterClass { public class InnerClass { ... }}Motivating ExampleMyListpublic class MyList { private Object [ ] a; private int size;}Need an iterator for MyListMyIterator Designpublic class MyIterator { 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++]; }}MyIterator DesignProblemsNeed to maintain reference to MyListNeed to access private data in MyListSolutionDefine MyIterator as inner class for MyListMyIterator DesignCode public class MyList { private Object [ ] a; private int size; public class MyIterator { private int pos; MyIterator() { pos = 0; } public boolean hasNext() { return (pos < size); } public Object next() { return a[pos++]; } }}Inner ClassesInner class instanceHas association to an instance of outer classMust be instantiated with an enclosing instanceIs tied to outer class object at moment of creation (can not be changed)MyList MyListMyIterator MyIterator MyIteratorInner Classes ExampleCodepublic class OC { // outer class private int x = 2; // don’t forget private public class IC { // inner class int z = 4; public int getSum() { return x + z; } }}Inner Classes ExampleClass referencing syntaxOuterClass.InnerClass ExampleOC 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 versaic.getSum() yields 6 // can access private x in oc!Accessing Outer ScopeCodepublic class OC { // outer class int x = 2; public class IC { // inner class int x = 6; public void getX() { // inner class method int x = 8; System.out.println( x ); // prints 8 System.out.println( this.x ); // prints 6 System.out.println( OC.this.x ); // prints 2 } }}Instantiating Inner ClassCommon gimmickOuter class method returns instance of inner classUsed by Java Collections Library for IteratorsCodepublic 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();Anonymous Inner ClassProperties Inner class without nameInstance of class returned by methodSyntaxnew ReturnType() { // unnamed inner class body of class… // implementing ReturnType} ;Anonymous Inner ClassCodepublic class MyList { public Iterator iterator() { return new Iterator() { // unnamed inner class … // implementing Iterator } ; }}MyList m = new MyList();Iterator it =
View Full Document