Previous | Home | Next |
Anonymous classes in the Java programming language are the classes without names thus, we can say, an anonymous class is essentially a local class without a name. The main advantage of using an anonymous class is Encapsulation.
An anonymous class is, in a way, the ultimate in private object oriented encapsulation. It is defined exactly where it is needed, it can never be used anywhere else, and it is having a total local scope. Anonymous classes provide a very clear syntax for implementing event listeners in GUI programming, and are very much useful for implementing method parameters and returning objects.
One of the key advantages of using anonymous classes is that it relieves the programmer from defining large numbers of special purpose classes.
Such classes are used only once and are defined right where the action is taking place, and never used again. Also, anonymous classes have access to all data and methods of their containing classes, including private members which means that for small highly localized tasks, they generally need less initialization.
The syntax of anonymous classes have one of the two forms which are shown below :
-
Interface Based : The syntax for declaring interface based anonymous class is given as,
-
Class Based Anonymous Class : The syntax for class based anonoymous class is,
new InterfaceName() { // ClassBody }
An interface based anonymous class has to implement the entire interface on which it is based upon, to understand it better, take a look at the following code snippet :
Example
Java Tutorialslass AnonymousClassExample { public static void main(String args[]) { Runnable obj = new Runnable() // anonymous class is defined here { private static final int ARRAY_LIMIT = 50; private int[] arr = new int[ARRAY_LIMIT]; // instance intialization code { for(int i=0; i< arr.length; i++) arr[i] = i; } public void run() { int sum = 0; for(int i=0; i<ARRAY_LIMIT; i++) sum = sum + arr[i]; System.out.println("The sum of the array elements is :"+sum); // printing the sum of array } }; // anonymous class ends here obj.run(); } }
new ClassName (optional Argument_List) { // Class Body }
A class based anonymous class has access to its base class methods and members and it can override it’s members, just like any other subclass. To understand more take a look at this code snippet,
Example
Java Tutorialslass AnonymousClassExample2 { ShowMessage anonymous_obj = new ShowMessage("R4R Example") { public void display() { System.out.println("anonymous override"+"member display ShowMessage Output :"+msg); } }; public AnonymousClassExample2() { ShowMessage obj = new ShowMessage("hi!!!!!!"); obj.display(); ShowMessage anonymous_obj2 =new ShowMessage("hi!!!!!!!") { public void display() { System.out.println("anonymous override"+"local display ShowMessage output :"+msg); } }; anonymous_obj2.display(); this.anonymous_obj.display(); } public static void main(String args[]) { AnonymousClassExample2 example = new AnonymousClassExample2(); } } Java Tutorialslass ShowMessage { String msg=""; public ShowMessage(String msg) { this.msg=msg; } public void display() { System.out.println("SimpleClass output :"+msg); } }
Previous | Home | Next |