Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Anonymous classes

 
Venkat Ramsimha
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all
can anybody provide WHT IS ANONYMOUS CLASS AND NEED SOME SAMPLE PROGRAMS ON HOW ANONYMOUS CLASSES CAN BE DECLARED?

thanks,
venkat
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A search of this board for "anonymous" provides a plethora of examples. But here's a crash course on the basics.

Suppose you have a class called MyClass. To create an instance, you would say...

MyClass my = new MyClass();

Now, suppose you inserted an alternative class body before the semicolon...

MyClass my = new MyClass() {...};

In this case, the alternative body is that of an anonymous class that extends MyClass. (If MyClass were an interface, then the anonymous class would implement the interface.) Also, the instance reference is automatically upcast to the type MyClass. Therefore, when methods are called on this instance, polymorphism will invoke the overridden versions defined in the anonymous class body.
[ May 11, 2005: Message edited by: marc weber ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic