Unformatted text preview:

OOP in Java Inner Classes Fawzi Emad Chau Wen Tseng Department of Computer Science University of Maryland College Park Package Package Group of related classes under one name Helps manage software complexity Import Make classes from package available for use Java API java core javax optional Example package edu umd cs name of package import java util Random import single class import java util all classes in package class definitions Scope Scope Part of program where a variable may be referenced Determined by location of variable declaration Types Top level classes Declared inside package Visible throughout package Nested classes Declared inside class or method Visible only inside class Scope Example Example Package Class Class Method Method package edu umd cs public class MyClass1 public void MyMethod1 public void MyMethod2 public class MyClass2 Scopes Inner Classes Description Class defined in scope of another class May be named or anonymous Property Can directly access all variables methods of enclosing class including private fields methods Inner Classes Useful for Logical grouping of functionality Data hiding Linkage to outer class Example public class OuterClass public class InnerClass Motivating Example MyList public class MyList private Object a private int size Need an iterator for MyList MyIterator Design public 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 Design Problems Need to maintain reference to MyList Need to access private data in MyList Solution Define MyIterator as inner class for MyList MyIterator Design Code 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 Classes 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 MyIterator MyIterator MyList MyIterator Inner Classes Example Code public 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 Example Class referencing syntax OuterClass InnerClass Example OC 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 versa ic getSum yields 6 can access private x in oc Accessing Outer Scope Code public 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 Class Common gimmick Outer class method returns instance of inner class Used by Java Collections Library for Iterators Code public 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 Class Properties Inner class without name Instance of class returned by method Syntax new ReturnType unnamed inner class body of class implementing ReturnType Anonymous Inner Class Code public class MyList public Iterator iterator return new Iterator unnamed inner class implementing Iterator MyList m new MyList Iterator it m iterator


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
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 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?