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.

Slides:



Advertisements
Similar presentations
PHP functions What are Functions? A function structure:
Advertisements

Introduction to classes Sangeetha Parthasarathy 06/11/2001.
DATA STRUCTURES Lecture: Interfaces Slides adapted from Prof. Steven Roehrig.
Programming With Java ICS201 University Of Hail1 Chapter 13 Inner Classes.
Unit 08 & 091 Nested Classes Introduction Inner Classes Local Classes Anonymous Classes Exercises.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
Road Map Introduction to object oriented programming. Classes
Java & Inner Classes L. Grewe. Kinds of of Classes Top level classes Top level classes Declared inside packageDeclared inside package Visible throughout.
Enhancing classes Visibility modifiers and encapsulation revisited
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
OOP Java1 Event Handling Overview Listeners, Adapters and Event Sources Inner classes Event Handling Details Applets and GUI Applications Event.
Inner Classes. Lecture Objectives Learn about inner classes. Know the differences between static and non- static inner classes. Designing and using inner.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
OOP in Java – Inner Classes Nelson Padua-Perez William Pugh 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.
ACM/JETT Workshop - August 4-5, ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Chapter 8: Inner Classes. You're an OO programmer, so you know that for reuse and flexibility/extensibility you need to keep your classes specialized.
CS 11 java track: lecture 4 This week: arrays interfaces listener classes inner classes GUI callbacks.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
Lecture 20 April 13, Review for Exam All Exercises and Sample Code from Lectures Object Based Programming Object Oriented Programming String and.
CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.
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.
Java Inner Classes. Interfaces interface KeyListener { public void keyPressed(KeyEvent e); public void keyReleased(KeyEvent e); public void keyTyped(KeyEvent.
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.
Advanced Programming Rabie A. Ramadan vpro/ Lecture 4.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Nested Classes CompSci 230 S Software Construction.
More Inheritance and More Java Stuff Opening Discussion zWhat did we talk about last class? zHave you read the description of assignment #2?
Programming in java Packages Access Protection Importing packages Java program structure Interfaces Why interface Defining interface Accessing impln thru.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
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.
1 DemoBasic_v3, DemoBasic_v4 JButton JLabel. 2 Registering an ActionListener Register by invoking the following from within constructor DemoBasicFrame.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Today Enumerated Types - Summary. Start Nested Classes: Inner Classes, Local Classes, Anonymous Classes, Lambda Functions Next: Interfaces, Abstract Classes.
Lecture 15.1 Event Delegation.
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.
CompSci 230 S Software Construction
Classes (Part 1) Lecture 3
Java Programming: Guided Learning with Early Objects
Chapter 8 Classes and Objects
CompSci 230 S Programming Techniques
Nested class.
Inner Classes 11/14/ Dec-04 inner_classes.ppt.
Overloading and Overriding
Packages and Interfaces
Classes & Objects: Examples
Inner Classes 29-Nov-18.
User-Defined Classes and ADTs
Java Inner Classes.
Chapter 8 Classes User-Defined Classes and ADTs
Constructors, GUI’s(Using Swing) and ActionListner
Inner Classes 17-Apr-19.
Inner Classes 21-Apr-19.
Inner Classes 1-May-19.
Inner Classes 11-May-19.
Inner Classes 18-May-19.
Chapter 15 Event-Driven Programming and Animations Part 1
Inner Classes.
Inner Classes 25-Oct-19.
Chapter 5 Classes.
Presentation transcript:

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. –at most one public per file –arbitrarily many package-scope per file –either package or public ONLY  Nested classes introduced in jdk1.1

Why use nested classes?  Simplifies many coding tasks –can define small classes on the fly near the objects created from them + concise syntax –can access outer classes iv’s automatically – no need to pass a this pointer to the constructor of separate outer class –can be hidden from other classes in the same package  However, price to pay in terms of complexity, number of gotchas, etc.

Pre jdk1.1  In jdk1.0, the clean and simple class rules were ballyhooed as a major improvement over C++  Addition of inner classes complicates things significantly  However, they do make certain code much less awkard to write, particularly when writing GUIs  Still, you do not have to use them, but they can be quite cool and I do recommend it in moderation!

Types of nested classes  Inner classes –local anonymous or named –non-local named only  Static nested classes –non-local named only

Non-local inner classes  Simply a nested class that does not have the static attribute and is not defined within a class method.  Can be private, public, package, protected, abstract, etc. just like any class member.  Think of outer class as owning inner class – inner class can only be instantiated via outer class reference (including this)  Inner class has access to all outer class iv’s, private or otherwise!

Simple non-local inner class example class Outer{ private int x1; Outer(int x1){ this.x1 = x1; } public void foo(){ System.out.println(“fooing”);} public class Inner{ private int x1 = 0; void foo(){ System.out.println(“Outer value of x1: “ + Outer.this.x1); System.out.println(“Inner value of x1: “ + this.x1); }

Simple example, cont -- driver Rules for instantiation a little funny public class TestDrive{ public static void main(String[] args){ Outer outer = new Outer(); // can create in regular way Inner inner = outer.new Inner(); //must call new through //outer object handle inner.foo(); // note that this can only be done if inner is visible // according to the regular scoping rules }

Inner class rules  Note that inner class can access outer class instance variables (even private ones).  It does this using the object reference.this  Refer to public inner class as.

Another example public class TalkingClock{ private int interval; private boolean beep; public TalkingClock(int interval, boolean beep){ …} public void start(){ …} public class TimePrinter implements ActionListener //inner class { …} }

When to use non-local inner classes  Most typically used when inner class is instantiated from outer class.  If classes naturally “belong together”, it is cumbersome to pass a this pointer to a separate outer class just so second class can access first class’s properties/methods.  Note that inner class can access outer class’s private data, making them even more powerful than mechanism implied above!

Local inner classes  Inner classes may also be defined within class methods.  These are called local inner classes.  Principle advantage is scoping: such classes are completely inaccessible anywhere but the method itself where they are defined.  Thus, they have no visibility attribute (public, etc.)  Also, can NOT access local variables other than those declared with final attribute.

Example: define TimePrinter in start() method public void start(){ class TimePrinter implements ActionListener{ public void actionPerformed(ActionEvent event){ Date now = new Date(); System.out.println(“At the tone, the time is “ + now); if (beep) Toolkit.getDefaultToolkit().beep(); } ActionListener listener = new TimePrinter(); Timer t = new Timer(interval, listener) t.start(); }

Local anonymous inner classes  Local inner classes can be taken a step further – it is not required to give them an explicit name.  This is very convenient when you want to use a class only once and the code that it contains is succinct.  Very common example is defining Swing callback functions.

Anonymous inner class example public void start(int interval, final boolean beep){ ActionListener listener = new ActionListener(){ public void actionPerformed(ActionEvent event){ Date now = new Date(); System.out.println("At the tone, the time is " + now); if (beep) Toolkit.getDefaultToolkit().beep(); } }; Timer t = new Timer(interval, listener); t.start(); }

Anonymous class example but.addActionListener( new ActionListener(){ public void actionPerformed(actionEvent ae){ //do work here } );