USE of ANONYMOUS CLASS IN JAVA
Anonymous class is an inner class without a name and for which only a single object is created. An anonymous inner class can be useful when making an instance of an object with certain “extras” such as overloading methods of a class or interface, without having to actually subclass a class.
Watch this video on our YouTube channel for better understanding, else, keep reading.
There are two ways to create Anonymous inner :
- Class (may be abstract or concrete)
Anonymous Inner Class example:
This program shows how to override the method of a class using anonymous inner class.
Anonymous Inner Class as Argument:
This program shows how to pass an anonymous inner class as a method argument.
Important facts about concerning anonymous classes:
- An anonymous class cannot have a constructor. so, there is no need to pass parameters to an anonymous class while instantiating it.
- Can be instantiated only once.
- Cannot be static.
- Has no name.
Hope this article helps you understand the concept of anonymous inner classes in Java programming language better. For more such content, subscribe to our blog.
Like & follow us on Facebook – https://www.facebook.com/pristinetechschool/
Let us know if you have any suggestions/feedback for us in the comment section below.