DOC PREVIEW
UMBC CMSC 331 - Inner Classes

This preview shows page 1-2-3-4 out of 12 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

JavaInterfacesFour kinds of inner classesMember classesMember classes IIAnonymous inner classesExample anonymous inner classStatic member classesLocal classesInterfaces, againAdaptersThe EndJavaInner ClassesInterfacesinterface KeyListener { public void keyPressed(KeyEvent e); public void keyReleased(KeyEvent e); public void keyTyped(KeyEvent e);}•An interface describes methods but does not supply bodies for them.Four kinds of inner classes•Member classes–simple•Anonymous classes–syntax is ugly but anonymous classes are useful•Static member classes (not too useful)•Local classes (not too useful)•Every class compiles to a separate .class fileMember 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 useful for handling events•Button b = new Button (“Click Me”);b.addActionListener (new Clicker ( ));…class Clicker implements ActionListener { … }•Can access the variables of the outer classAnonymous inner classes•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 interfaceExample anonymous inner classb.addActionListener (new ActionListener ( ) { public void actionPerformed (ActionEvent e) { System.out.println (“Ouch!”);} } ) ;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•Inner classes did not exist in Java 1.0Local classes•A local class is a class defined inside a method.•A local class cannot access variables declared in the method (!)•There are many other restrictions on local classes.Interfaces, againinterface KeyListener { public void keyPressed(KeyEvent e); public void keyReleased(KeyEvent e); public void keyTyped(KeyEvent e);}Adaptersclass KeyAdapter implements KeyListener { public void keyPressed(KeyEvent e) { }; public void keyReleased(KeyEvent e) { }; public void keyTyped(KeyEvent e) { };}The


View Full Document

UMBC CMSC 331 - Inner Classes

Documents in this Course
Semantics

Semantics

14 pages

Java

Java

12 pages

Java

Java

31 pages

V

V

46 pages

Semantics

Semantics

11 pages

Load more
Download Inner Classes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Inner Classes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Inner Classes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?