DOC PREVIEW
UMD CMSC 132 - OOP in Java – Inner Classes

This preview shows page 1-2-3-4-5-6 out of 17 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

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

UMD CMSC 132 - OOP in Java – Inner Classes

Documents in this Course
Notes

Notes

8 pages

Recursion

Recursion

12 pages

Sorting

Sorting

31 pages

HTML

HTML

7 pages

Trees

Trees

19 pages

HTML

HTML

18 pages

Trees

Trees

19 pages

Honors

Honors

19 pages

Lecture 1

Lecture 1

11 pages

Quiz #3

Quiz #3

2 pages

Hashing

Hashing

21 pages

Load more
Download OOP in Java – Inner Classes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view OOP in Java – Inner Classes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view OOP in Java – Inner Classes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?