AnonDemo.java
Download AnonDemo.java
1: // Demonstration of a simple (if that's possible) use of an anonymous
2: // local class. (Note that anonymous classes really do have names. In
3: // this case, the class gets the name "AnonDemo$1".)
4: //
5: // Written 11/25/2014 by Wayne Pollock, Tampa Florida USA
6:
7: /** Class Greet is a simple class with one method, that displays a greeting:
8: */
9: class Greet {
10: public void sayHello( String name ) {
11: System.out.println( "Hello, " + name + "!" );
12: }
13: }
14:
15: class AnonDemo {
16: public static void main ( String[] args ) {
17:
18: // Create a Greet object, and invoke its method:
19: Greet g = new Greet();
20: g.sayHello( "Jordan" );
21:
22: // Create an object of an anonymous class that extends class Greet,
23: // and overrides the sayHello method to say "Good Night" instead:
24: Greet gn = new Greet() {
25: public void sayHello( String name ) {
26: System.out.println( "Good night, " + name + "." );
27: }
28: };
29: gn.sayHello( "Wayne" );
30: }
31: }