OOP in Java – Inner Classes Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.

Slides:



Advertisements
Similar presentations
Introduction to classes Sangeetha Parthasarathy 06/11/2001.
Advertisements

Inner Classes. Nested Classes  An nested class is a class that is defined inside another class.  To this point we have only studied top-level classes.
Programming With Java ICS201 University Of Hail1 Chapter 13 Inner Classes.
Inner Classes in Java CMSC 432 Shon Vick. 2 Conceptual Structure package p 1 class A 1 class A 2... class A n... package n class A 1 class A 2... class.
OOP in Java – Inner Classes Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Nested Classes Yoshi Modified from:
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
Unit 08 & 091 Nested Classes Introduction Inner Classes Local Classes Anonymous Classes Exercises.
Road Map Introduction to object oriented programming. Classes
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
Java & Inner Classes L. Grewe. Kinds of of Classes Top level classes Top level classes Declared inside packageDeclared inside package Visible throughout.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Inner Classes. Lecture Objectives Learn about inner classes. Know the differences between static and non- static inner classes. Designing and using inner.
Inner Classes & the True Nature of Static. Administrivia Today: R2 Next Wed (Mar 30): Quiz 3 Multithreading and synchronization JDK java.lang.Thread,
Unit 081 Introduction to Nested Classes Nested classes are classes defined within other classes The class that includes the nested class is called the.
Nested Classes OOP tirgul No Nested Classes Java allows you to define Nested Classes public class EnclosingClass { public int _dataMember = 7;
OOP in Java – Inner Classes Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.
Interfaces and Inner Classes. What is an Interface?  What is “presented to the user”?  The public part of a class?  What is the substance of an interface?
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Inner Classes. Inner classes All the classes so far have been “top level” It is possible (and useful) to define a class inside another class Inner classes.
Inner Classes in Java. Overview Inner class – one class nested inside another – static (i.e. class) rarely if ever used – member (i.e. instance) – anonymous.
Java CourseWinter 2009/10. Introduction Object oriented, imperative programming language. Developed: Inspired by C++ programming language.
CS2110 Recitation 07. Interfaces Iterator and Iterable. Nested, Inner, and static classes We work often with a class C (say) that implements a bag: unordered.
COMP More About Classes Yi Hong May 22, 2015.
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 8, page 1 Sun Certified Java 1.4 Programmer Chapter 8 Notes Gary Lance
Lecture 10 Documentation, Garbage Collection, and Nested Classes/Interfaces.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
2-Dec-15 Inner Classes. 2 Inner classes All the classes so far have been “top level” It is possible (and useful) to define a class inside another class.
2-Dec-15 Inner Classes By Alguien Soy. 2 Inner classes All the classes so far have been “top level” It is possible (and useful) to define a class inside.
1 TCSS 143, Autumn 2004 Lecture Notes Inheritance.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
An Advanced Code Pattern: Inner Classes CSE301 University of Sunderland Harry R. Erwin, PhD Half Lecture.
Access Modifiers Control which classes use a feature Only class-level variables may be controlled by access modifiers Modifiers 1. public 2. protected.
Nested Classes CompSci 230 S Software Construction.
(1) ICS 313: Programming Language Theory Chapter 11: Abstract Data Types (Data Abstraction)
Iterators, Iterator, and Iterable 2015-T2 Lecture 8 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Thomas Kuehne.
Swing Threading Nested Classes COMP 401 Fall 2014 Lecture 23 11/25/2014.
Object Oriented Programming in Java Habib Rostami Lecture 10.
Advanced Java class Nested Classes & Interfaces. Types of Nested Classes & Interfaces top-level nested –classes –interfaces inner classes –member –local.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 26, 2009.
N ESTED C LASSES -1. T YPES OF N ESTED C LASSES & I NTERFACES top-level nested classes interfaces inner classes member local named anonymous.
Inner Classes.
Topic: Inner Classes Course : JAVA PROGRAMMING Paper Code: ETCS-307 Faculty : Dr. Prabhjot Kaur Reader, Dept. of IT 1.
Inner Classes 27-Dec-17.
EKT472: Object Oriented Programming
CompSci 230 S Software Construction
CompSci 230 S Programming Techniques
Road Map Introduction to object oriented programming. Classes
Nested class.
Inner Classes 11/14/ Dec-04 inner_classes.ppt.
Android Programming Lecture 2
Inner Classes 29-Nov-18.
Lecture 26: Advanced List Implementation
Java Inner Classes.
Simple Classes in Java CSCI 392 Classes – Part 1.
Inner Classes 17-Apr-19.
Inner Classes 21-Apr-19.
Inner Classes 1-May-19.
CSE 341 Lecture 11 b closures; scoping rules
Inner Classes 11-May-19.
Inner Classes 18-May-19.
Inner Classes.
CSG2H3 Object Oriented Programming
Inner Classes 25-Oct-19.
Chapter 5 Classes.
Presentation transcript:

OOP in Java – Inner Classes Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park

Scope of Classes Top level classes Declared inside package Visible throughout package Nested classes Declared inside class (or method) Visible only inside class

Inner Classes Description Class defined in scope of another class Property Can directly access all variables & methods of enclosing class (including private fields & methods) Example public class OuterClass { public class InnerClass {... }

Inner Classes May be named or anonymous Useful for Logical grouping of functionality Data hiding Linkage to outer class Examples Iterator for Java Collections ActionListener for Java Swing

Motivating Example MyList public class MyList { private Object [ ] a; private int size; } Need an iterator for MyList

MyIterator Design public class MyIterator implements Iterator { 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 implements Iterator { 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

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();