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.

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.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Agenda –interfaces and realization –type hierarchy –introduction to graphics and event handling.
Nested Classes Yoshi Modified from:
Lecture 17 Abstract classes Interfaces The Comparable interface Event listeners All in chapter 10: please read it.
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.
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.
1 Inner Classes Overview  Java Inner Classes: A Definition.  Overview of Nested Classes.  Top level Nested Classes.  Non-static Inner Classes.  Non-static.
Unit 081 Introduction to Nested Classes Nested classes are classes defined within other classes The class that includes the nested class is called the.
Lecture 18 Review the difference between abstract classes and interfaces The Cloneable interface Shallow and deep copies The ActionListener interface,
1 Introduction to Searching and Sorting Comparable Interface -Reading p Comparator Interface.
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.
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.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
CS 11 java track: lecture 4 This week: arrays interfaces listener classes inner classes GUI callbacks.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
Comp 249 Programming Methodology Chapter 13 Interfaces & Inner Classes Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
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.
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.
24-Dec-15 Class Structure. 2 Classes A class describes a set of objects The objects are called instances of the class A class describes: Fields (instance.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Interfaces and Inner Classes
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.
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:
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Basic Syntax อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 2.
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.
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.
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.
NESTED CLASSES REFLECTION PROXIES.
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
Nested class.
Inner Classes 11/14/ Dec-04 inner_classes.ppt.
Anonymous Classes A short-cut for defining classes when you want to create only one object of the class.
Overloading and Overriding
Packages and Interfaces
Inner Classes 29-Nov-18.
Class Structure 7-Dec-18.
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.
Inner Classes.
...that can get messy when we have lots buttons!
Inner Classes 25-Oct-19.
Presentation transcript:

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 another class The usual access modifiers ( public, protected, private ) can be used Inner classes were not in Java 1.0 They had to be added in later As a result, inner classes are not as well done as some other aspects of the language

3 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

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

5 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! Member classes are very easy Declare them where you would declare a field or a method

6 Local classes A local class is a class defined inside a method Like any other local declarations, the class declaration is available only within that method However, objects created from that local class can “escape” the class by being assigned to nonlocal variables Because its instances may exist after the method exits, code in the local class cannot access variables declared in the method unless they are declared final This makes them practically useless A local class can have access to private variables of the enclosed class

Local classes II class LocalClassExample{ private String name = "KFUPM"; public void method ( ) { int j = 20; final int k = 30; class Local { public void test ( ) { //System.out.println(j); //Error as j is not final System.out.println(k); //OK k is final //Like an inner class, instance variables of //the enclosing object can be accessed. System.out.println ( name ) ; } } Local loc = new Local ( ) ; loc.test ( ) ; } public static void main ( String [ ] args ) { LocalClassExample obj = new LocalClassExample ( ); obj.method ( ) ; } } 7

8 Anonymous inner classes It is a local class without a name. Anonymous inner classes are convenient for short code (typically a single method) b.addActionListener( anonymous inner class ); The anonymous inner class can be either: new Superclass ( args ) { body } or new Interface ( args ) { body } Notice that no class name is given--only the name of the superclass or interface If it had a name, it wouldn’t be anonymous, now would it?

9 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

Example anonymous inner class II 10

11 Static member classes static class StaticMember { … } 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 Static member classes cannot extends from non-static inner class

Static member classes II LinkedList.java 12

13 Summary Member classes An ordinary class, just defined within another Has full access to the variables of the enclosing class Anonymous classes Useful for short Listeners used in only one place Has full access to the variables of the enclosing class Static member classes Defined inside another class, but acts like an outer class Local classes Defined within a method Can access final variables in the enclosing class

14 The End