120 likes | 306 Views
Interfaces. interface 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 classessimpleAnonymous classe
E N D
1. Java Inner Classes
2. Interfaces An interface describes methods but does not supply bodies for them.
3. 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 file
4. 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 ( ); }}
5. 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 class
6. Anonymous 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 interface
7. Example anonymous inner class
8. 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.0
9. Local 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.
10. Interfaces, again
11. Adapters
12. The End