Anonymous Inner Classes in Java

|
| By Webner

Using Anonymous Inner Classes in Java:

Before getting into the depth of Anonymous inner classes, let’s understand a bit about inner classes first. As the name implies, a class which is declared and defined inside another class just like a normal member function or variable is called inner class. The inner class is usually defined as a private member of the container class (also called as the outer class). Hence, it can’t be directly accessed from the outside world. Only the outer class can access its properties and functions.

Example of inner class:

OuterClass.java

public class SampleOuterClass { //outer class
    private int val = 20;
    public void showMethod() {
        System.out.println("Value of variable in outer class= " + val);
    }

    private class SampleInnerClass { //inner class
        private int val = 30; // local copy of val variable for inner class
        public void display() {
            System.out.println("Value of variable in inner class= " + val);
        }
    }

    public void showInnerMethod() {
        SampleInnerClass inner = new SampleInnerClass(); //creating object of inner class
        inner.display();
        showMethod();
    }
}

TestInnerClass.java

public class TestInnerClass {

    public static void main(String[] args) {
        // creating object of outer class 
        SampleOuterClass outer = new SampleOuterClass();

        // call showInnerMethod to access inner class
        outer.showInnerMethod();
    }
}

Output:

Value of variable in inner class= 30
Value of variable in outer class= 20

An anonymous inner class is a type of inner class which is defined and declared within a class but, without a class name. That is, the class which is going to be created, is anonymous because it doesn’t have any name. Hence, instead of creating it as a class, we directly instantiate it without using any name. All we have it the instance variable of this class. It is generally used if you need to override methods of any class or an interface.

Advantages of using the anonymous inner class:

1. Using anonymous inner class we can declare and instantiate a class at the same time.
2. The anonymous inner classes are most suitable to implement those interfaces or superclasses where only a small amount of functionality and logic is required to be overridden.
3. These are easy to create as compared to creating a separate class. Having said that, instead of creating a new class for implementing an interface which contains only one or two methods, using anonymous inner class is the quicker way to implement this type of interface.
4. Namespace consideration is an additional advantage as we don’t need to think of a new class name every time.
Making an inner class as anonymous makes sure that the class will not be used anywhere else. Whereas, an inner class can be used any number of times by the class that created it (and by other classes if the inner class is not declared as private).
5. Moreover, an anonymous inner class cannot have a constructor as it doesn’t have a name. So, if it is required to pass some variables apart from the those which are members of the class it is extending, then a named inner class should be used instead.

Below is the basic example of anonymous inner class:

interface Article {
    public void display();
}

public class Item {
    //defining anonymous inner class which implements the Article interface. The article is the instance name of anonymous inner class
    Article article = new Article() {
        public void display() {
            System.out.println("Inside anonymous inner class");
        }
    };
}

Explanation: Here, Article is the name of an Interface which is implemented by the anonymous inner class. Inside class Item,

Article article = new Article () { }

creates an instance of the anonymous inner class which implements Article interface without using ‘implements’ keyword. That is, using a single line of code we are implementing an interface and creating an object of the anonymous class at the same time. Here, article is the instance variable of the anonymous inner class. Inside this class, display() method is overridden.

This explains the basic concept of Inner classes and Anonymous Inner classes.

Leave a Reply

Your email address will not be published. Required fields are marked *