Skip to content

Anonymous Inner Class

January 16, 2008
tags:

Meri bheegi bheegi si palkon pe rah gaye, jaise mere sapne bikhar ke – ANAMIKA“. Indian readers can find the relevance here 🙂

An anonymous inner class is one which does not have any name. Anonymous inner classes are created on the fly just in time. They introduce the weirdest looking java syntax. Following code shows an example of the anonymous inner class:

class Test
{

public void foo()
{

System.out.println(“main class foo method”);

}

public static void main(String[] args)
{

Test t = new Test()
{

public void foo()
{

System.out.println(“Anonymous class’s foo method”);

}

};

t.foo();

}

}

In this case look at the line in the main method that creates the instance of the Test class. Isn’t the syntax looking weired. Whats happening here is that we are actually creating an anonymous class which is a subclass of the Test class and overriding the foo() method. This is very useful when you just require to override one or two methods of a class, but do not want to create a new class all together for it. Here the anonymous class is created just-in-time and when the above program is run, it prints “Anonymous class’s foo method”.

Anonymous inner classes can also be used as an argument to a method call. The following code shows the same:

class Test
{

public void foo(Test t)
{

t.foo();

}

public void foo()
{

System.out.println(“main class’s foo called”);

}

public static void main(String[] args)
{

Test t = new Test();

t.foo(new Test()
{

public void foo()
{

System.out.println(“Anonymous class’s foo method”);

}

});

}

}

In the above we are calling an overloaded foo() method that takes an object of the Test class. We want to pass in an object that has a different behavior for the foo() method. So what we do is we create a just-in-time anonymous inner class and pass it as an argument to overloaded foo method. So when the no-arg foo method is called on the Test object from within the overloaded foo method, the anonymous inner class’s version of the no-arg foo method is called instead of the Test class’s original no-arg foo method. When this program is run it prints “Anonymous class’s foo method”

I would like to discuss one more case of the anonymous inner class case here. Sometimes you might require passing a just-in-time implementation of an interface which is not yet created. Consider a scenario that you have an interface and you do not have any class implementing that interface. You want to call a method that accepts the interface type. hat do you do? Simple ion this case also you would create an anonymous inner class and pass it as an argument to that method. The following code shows you the same:

class Test
{

public void foo(AnInterface i)
{

i.method1();

}

public static void main(String[] args)
{

Test t = new Test();t.foo(new AnInterface()

{

public void method1()
{

System.out.println(“Created just in time implementation class of the AnInterface”);

}

});

}

}

interface AnInterface
{

void method1();

}

Here we just created an anonymous inner class that is an implementation of the AnInterface interface and passed it to the method foo which takes the AnInterface type as a parameter. You can see that the anonymous inner class has to give the implement the method1() present in the AnInterface (obviously 🙂 ). When this program is run, it prints “Created just in time implementation class of the AnInterface”.

This is it for the anonymous inner classes :-). Will keep coming up with more topics till the time I finish preparing for SCJP 🙂

Advertisements
4 Comments leave one →
  1. Nayan permalink
    March 12, 2010 4:46 am

    Well explained Sanjeev. Keep coming with more such complex topic in Java.

    Thanks,
    Nayan

  2. arc permalink
    September 9, 2010 3:05 am

    wooahh,,,
    great, article!
    easy to understand 🙂

  3. rajesh permalink
    September 13, 2010 8:13 am

    ulti mate .i have refeered so many web sites but i am not understand.but here some of my douts clarify.thank u

  4. ashu permalink
    July 2, 2012 7:45 am

    good job….was really helpful

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: