Monday, July 29

All about Anonymous Class


What is Anonymous class?

A class having no name is anonymous class. It falls in the family of nested classes in java. It is also called inner class

How is it different from a normal class?

1. It is not a member of enclosing class
2. Wherever it has to be used it is declared and instantiated there itself.
3. These classes can't extend any class and can't implement any interface.

What is the Use of Inner class?

You want to create a class on the fly. For example

to create comparators in a class where to want to sort objects list based on certain object properties

to create process objects such as Thread ,TimerTask,Runnable

These are very short lived and useful where we don't want classes or instances to live for long and wish to quickly use that and remove .

No comments:

Post a Comment