Java and J2EE Tutorials, Jsp and Servlet Tutorials, Spring MVC, Solr, XML, JSON Examples, Hibernate & Struts 2 Hello World projects



Friday, 11 October 2013

Anonymous inner class in Java - Example and Explanation

In our precious discussion we came across 'What are inner classes in Java'. In this particular blog we will look into a special case of Inner Classes i.e. Anonymous inner classes. In java ‘anonymous classes’ are also called ‘anonymous inner classes’, the fact behind this statement is that 'Anonymous classes' are always defined inside another class and there is no anonymous class without the word 'inner'.

In short we can say that an anonymous class is an inner class that does not have a name at all. And whose instance is being created at the time of its creation.


How to create an anonymous inner class in java

Anonymous classes in java are defined in a different way than that of a normal java classes. In java anonymous classes can be created in either of the two ways.

1) Using a class reference variable.
2) Using an interface.


1) Creating a anonymous inner class using a class’s reference variable.

In the code below we have declared a class with name ‘Apple’ and another class with name ‘Implementation’, there is nothing new in creation of these two classes. An strange thing that we did here, is we created an anonymous class inside ‘Implementation’. It looks like we are creating an object of ‘Apple’ in ‘Implementation’ but actually we are creating an subclass of apple along with an instance of this anonymous inner class. This way the new subclass of Apple does not contain a name and have an instance created automatically.

Here the thing to be concentrated twice is that, created anonymous class is a subclass of Apple class and hence holds all the properties that a subclass has.
package com.beingjavaguys.core;

/*
 *  Simple java class with two instance variables
 * 
 */
public class Apple {
 public void print() {
  System.out.println("Printed from Apple !");
 }

 public void printAgain() {
  System.out.println("Printed from Apple again !");
 }
}


package com.beingjavaguys.core;

/*
 *  Simple java class creation 
 */
public class Implementation {

 /*
  * This looks like we are creating an object of Apple class, but actually we
  * we have created an instance of anonymous subclass of Apple. The statement
  * written below creates an anonymous inner class with an instance of it and
  * the class is a subclass to Apple.
  */
 Apple apple = new Apple() {
  @Override
  public void print() {
   System.out.println("Printed from subclass of Apple !");
  }
 };
}


Anonymous inner class in java is a subclass

So what is happening exactly in the code above is that we are creating an object of Subclass with a reference variable of Superclass(Apple). Now this is the thing where Polymorphism comes into picture, from Subclass-Superclass Relationship we know that using ‘apple’ we can call only those members that are present in Apple(Superclass).
package com.beingjavaguys.core;

public class Apple {
 public void print() {
  System.out.println("Printed from Apple !");
 }
}


package com.beingjavaguys.core;

public class Implementation {

 Apple apple = new Apple() {
  @Override
  public void print() {
   System.out.println("Printed from subclass of Apple !");
  }

  /*
   * Not a Overriden method.
   */
  public void add() {
   System.out.println("I am not present in Apple !");
  }
 };

 public void r() {
  /*
   * This will work fine.
   */
  apple.print();

  /*
   * apple.add(); 
   * 
   * This will give a compilation error because, from a
   * superclass reference variable we can not call a member of subclass
   * that is not present in superclass.
   */

 }
}


2) Creating an anonymous inner class from an interface.

Now what is happening exactly in the code below is that we have created a anonymous inner class that implements AppleInterface and an instance of this class at the same time. This is the only condition in java, where we have implemented an interface without even having a name of the class and nor using implement keyword.
package com.beingjavaguys.core;

public interface AppleInterface {
 public void callMe();

}

package com.beingjavaguys.core;

public class Implementation {

 /*
  * Here we have created an anonymous inner class that implements
  * AppleInterface and also created an instance of this class at the same
  * time.
  */
 AppleInterface appleInterface = new AppleInterface() {

  @Override
  public void callMe() {
   // TODO Auto-generated method stub

  }
 };
}



Why to use an anonymous inner class in java.

Till now we have seen that, we have created a class ‘Apple; and than an anonymous inner class to override a method ‘print()’. This thing can be done by creating a subclass of ‘Apple’ using ‘extends’ keyword, than why do we need to create a anonymous class?

The answer to the question is, creating anonymous class is quicker and simple. Anonymous inner classes are useful when we need to inherit a few properties (only one method) of a superclass and this is not a good idea to take overhead of creating a separate subclass for doing things so simple.


Points to remember about anonymous classes in Java

1) Anonymous classes are just like local classes, besides they don’t have a name. In Java anonymous classes enables the developer to declare and instantiate a class at the same time.
2) In java normal classes are declaration, but anonymous classes are expressions so anonymous classes are created in expressions.
3) Like other inner classes, an anonymous class has access to the members of its enclosing class.


Here we are done with an example and explanation of anonymous inner classes in java, in our upcoming blogs we will see more about Java Programming and other opensource technologies.








Thanks for reading !
Being Java Guys Team




4 comments:

  1. Thanks for sharing this concept, as this was my long time doubt. You cleared it very well.

    Regards,

    hadoop training
    in chennai

    ReplyDelete

Like Us on Facebook


Like Us On Google+



Contact

Email: neel4soft@gmail.com
Skype: neel4soft