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.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Programming With Java ICS201 University Of Hail1 Chapter 13 Inner Classes.
JFrame JComponent JFrame JComponent JFrame JComponent.
Agenda –interfaces and realization –type hierarchy –introduction to graphics and event handling.
Event-Driven Programming Thus far, our programs have been executed one statement after the other However, many programs depend on user actions to dictate.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 13 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.
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;
Lecture 18 Review the difference between abstract classes and interfaces The Cloneable interface Shallow and deep copies The ActionListener interface,
OOP in Java – Inner Classes Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.
CSE 331 Software Design & Implementation Dan Grossman Spring 2015 GUI Event-Driven Programming (Based on slides by Mike Ernst, Dan Grossman, David Notkin,
ACM/JETT Workshop - August 4-5, ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
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
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.
Dale Roberts GUI Programming using Java - Event Handling Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
CSE 501N Fall ‘09 20: Event Handling and Inner Classes 17 November 2009 Nick Leidenfrost.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
Session 10 CannonGame and Event-driven Programming.
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
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.
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.
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:
What Is an Event? Events – Objects that describe what happened Event sources – The generator of an event Event handlers – A method that receives an event.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Object Oriented Programming in Java Habib Rostami Lecture 10.
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
Advanced Java class Nested Classes & Interfaces. Types of Nested Classes & Interfaces top-level nested –classes –interfaces inner classes –member –local.
CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside.
1 DemoBasic_v3, DemoBasic_v4 JButton JLabel. 2 Registering an ActionListener Register by invoking the following from within constructor DemoBasicFrame.
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.
GUI Programming using Java - Event Handling
Inner Classes.
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
GUI III IS
CompSci 230 S Programming Techniques
Nested class.
Inner Classes 11/14/ Dec-04 inner_classes.ppt.
Overloading and Overriding
Packages and Interfaces
Inner Classes 29-Nov-18.
Interfaces.
Java Inner Classes.
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.
CiS 260: App Dev I Chapter 6: GUI and OOD.
Inner Classes 18-May-19.
Session 29 Introducing CannonWorld (Event-driven Programming)
Inner Classes.
...that can get messy when we have lots buttons!
Inner Classes 25-Oct-19.
Presentation transcript:

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 were not in Java 1.0 –As a result, they are not as well done as some other aspects of the language

Four kinds of inner classes Member classes –Simple and useful Anonymous classes –Useful, but syntax is ugly Static member classes (not too useful) Local classes (not too useful) Every class compiles to a separate.class file Inner classes compile to files with a $ in their names

Member classes A member class is an “ordinary” inner class class Outer { int n; class Inner { int ten = 10; void setNToTen ( ) { n = 10; } } void setN ( ) { new Inner ( ).setNToTen ( ); } }

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 { … } Can access the variables of the enclosing class –This is what makes them so useful! Member classes are very easy –Declare them where you would declare a field or a method

Anonymous inner classes Anonymous inner classes are convenient for short code b.addActionListener ( anonymous inner class ); The anonymous inner class can be either: new Superclass ( args ) { body } new Interface ( args ) { body } Notice that no class name is given--only the name of the superclass or interface

Example 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

Static member classes static class Inner { … } A static member class can access only static variables of the outer class A static member class isn't “really” an inner class, but a top-level class that happens to be written inside another class Static member classes are not too useful

Local classes A local class is a class defined inside a method A local class cannot access variables declared in the method (!) –This makes them practically useless There are many other restrictions on local classes

The End