Inner Classes 11/14/201828-Dec-04 inner_classes.ppt.

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.
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.
Inner Classes. Lecture Objectives Learn about inner classes. Know the differences between static and non- static inner classes. Designing and using inner.
OOP in Java – Inner Classes Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Inner Classes Overview  Java Inner Classes: A Definition.  Overview of Nested Classes.  Top level Nested Classes.  Non-static Inner Classes.  Non-static.
Nested Classes OOP tirgul No Nested Classes Java allows you to define Nested Classes public class EnclosingClass { public int _dataMember = 7;
James Tam An introduction into HCI: Task-Centered System Design An Introduction To Graphical User Interfaces The event-driven model Building a simple.
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.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 8, page 1 Sun Certified Java 1.4 Programmer Chapter 8 Notes Gary Lance
Object-Oriented Programming (OOP). Implementing an OOD in Java Each class is stored in a separate file. All files must be stored in the same package.
CS 11 java track: lecture 4 This week: arrays interfaces listener classes inner classes GUI callbacks.
CSE 501N Fall ‘09 20: Event Handling and Inner Classes 17 November 2009 Nick Leidenfrost.
Chapter 12 1 TOPIC 13B l Buttons and Action Listeners Window Interfaces Using Swing Objects.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.
Anonymous Classes An anonymous class is a local class that does not have a name. An anonymous class allows an object to be created using an expression.
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.
Object Oriented Programming.  Interface  Event Handling.
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
Abstract Classes and Interfaces 5-Dec-15. Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method.
An Advanced Code Pattern: Inner Classes CSE301 University of Sunderland Harry R. Erwin, PhD Half Lecture.
Nested Classes CompSci 230 S Software Construction.
A cannon game ?. Simple version angle from command line, one shot only Coordinate system is “upside-down”: Use dy(int) method to transform y coordinate:
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Event Handling CS 21a: Introduction to Computing I First Semester,
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.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Today Enumerated Types - Summary. Start Nested Classes: Inner Classes, Local Classes, Anonymous Classes, Lambda Functions Next: Interfaces, Abstract Classes.
Inner Classes.
CSC 205 Programming II Lecture 5 AWT - I.
Topic: Inner Classes Course : JAVA PROGRAMMING Paper Code: ETCS-307 Faculty : Dr. Prabhjot Kaur Reader, Dept. of IT 1.
CompSci 230 S Programming Techniques
Inner Classes 27-Dec-17.
CompSci 230 S Software Construction
Classes (Part 1) Lecture 3
“Form Ever Follows Function” Louis Henri Sullivan
Some Eclipse shortcuts
GUI III IS
CompSci 230 S Programming Techniques
Event Handling CS 21a: Introduction to Computing I
Nested class.
Anonymous Classes A short-cut for defining classes when you want to create only one object of the class.
Interface.
Overloading and Overriding
Inner Classes 29-Nov-18.
GUI building with the AWT
Class Structure 7-Dec-18.
Web Design & Development Lecture 13
Java Inner Classes.
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.
Session 29 Introducing CannonWorld (Event-driven Programming)
Inner Classes.
...that can get messy when we have lots buttons!
CSG2H3 Object Oriented Programming
Inner Classes 25-Oct-19.
Presentation transcript:

Inner Classes 11/14/201828-Dec-04 inner_classes.ppt

most classes we have seen are “top level” Inner classes most classes we have seen are “top level” it is possible (and useful) to define a class inside another class the usual access modifiers (public, protected, private) can be used inner classes were not in Java 1.0 11/14/201828-Dec-04

you may encounter inner classes in two ways member classes (also simply called “inner classes”) simply enclosed within another class typically not static often used as “listeners” anonymous inner classes also used as “listeners” but have no name syntax is ugly every class compiles to a separate .class file inner classes compile to files with a $ in their names 11/14/201828-Dec-04

a member class is an “ordinary” inner class member classes a member class is an “ordinary” inner class class Outer { int n; class Inner { int ten = 10; void setNToTen( ) { n = ten; } } void setN ( ) { new Inner( ).setNToTen( ); } } 11/14/201828-Dec-04

member classes II member classes are often used to handle events: Button b = new Button ("Click Me"); b.addActionListener (new Clicker( )); … class Clicker implements ActionListener { … } a member class can access the variables of the enclosing class this is what makes them so useful! declare them where you would declare a field or a method 11/14/201828-Dec-04

anonymous inner classes anonymous inner classes are convenient for short code (typically a single method) b.addActionListener( anonymous inner class ); notice that no class name is If it had a name, it wouldn’t be anonymous 11/14/201828-Dec-04

example of anonymous inner class an ActionListener is a Java-supplied interface for listening to Buttons and some other things the format (from the previous slide) is new Interface (args) { body } b.addActionListener ( new ActionListener( ) { public void actionPerformed (ActionEvent e) { System.out.println (“Ouch!”); } } ) ; like member classes, anonymous inner classes have full access to the fields and methods of the containing class however, Eclipse may not give you code-assist for them 11/14/201828-Dec-04

summary member (“inner”) classes anonymous classes an ordinary class, just defined within another (anywhere a method or field could be placed) has full access to the variables of the enclosing class this is what makes them useful! anonymous classes inner classes without a class name useful for short Listeners used in only one place 11/14/201828-Dec-04

the end 11/14/201828-Dec-04