This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Anonymous classes  RSS feed

 
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 ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!