Java Anonymous Inner Class Tutorial

In this section, we will learn what the anonymous inner class is and how to use it in Java.

Prerequisite: Java abstracts and interfaces.

What is anonymous inner class in Java?

In short, we use an anonymous class to fully define the body of interfaces and abstract classes to create object from them directly.

We know that we can’t create objects directly from interfaces and abstract classes because their body is not fully defined. We basically use another class to extend and fully implement an abstract class or interface and then use that third party to create an object.

The main reason that stops interfaces and abstract classes from directly being used to create objects from is their incomplete body.

But via anonymous classes, we can directly use interfaces and abstract classes to create objects.

Note: Anonymous class comes right where we want to create an object of an interface or an abstract class.

How do create anonymous class in Java:

new AbstractClass(){/*...*/}

new InterfaceName(){/*...*/}

new ClassName(){/*...*/}

As mentioned before, anonymous classes come where we want to create an object. We put them right after the pair of parentheses of the constructor.

Notes:

  • The pair of braces `{}` that comes after the parentheses representing the body of the anonymous class.
  • Inside the body of this anonymous class, we can fully define any abstract method or other necessary operations.
  • `ClassName` is a class that implemented an interface with abstract methods or extended an abstract class with abstract methods in it. Inside the body of the anonymous class we can fully define those abstract members so to be able to create objects.

Example: using anonymous inner class to implement an interface in Java

Create an interface named `Employee` and store the code below into that interface:

public interface Employee {

    void setName(String name);
    String getName();
}

This interface has two methods, named `setName` and `getName` that should be implemented in a class.

Now create a class named `Simple` and store the code below into that class:

public class Simple {

    public static void main(String[] args) {
        Employee emp  = new Employee() {
            private String name;
            @Override
            public void setName(String name) {
                this.name = name;
            }

            @Override
            public String getName() {
                return name;
            }
        };
        emp.setName("Jack");
        System.out.println(emp.getName());
    }
}

Output: Jack

We know that the `Employee` is an interface and needs to be implemented in a class first! So what we did here was to create an anonymous class and attach it to the Employee.

Anonymous class comes right where we want to create an object of an interface or an abstract class.

In this example, after the `new Employee()` we put the pair of braces `{}` and that represents the anonymous class.

What happened is that the `new Employee()` object is now supported by the anonymous class that we’ve declared here. So we can say this new object is of type anonymous class.

Java anonymous class notes:

  • Inside anonymous classes, we need to fully define the incomplete methods of the target abstract class or interface.
  • We can add member variables in anonymous classes. In our example, we added the private member `name`.
  • We can’t use constructors in anonymous classes.
  • An anonymous class cannot extend to other classes or implement other interfaces.
  • Every time we want to create an object directly from an interface or an abstract class, we need to set an anonymous class for it.
  • An anonymous class can have constant (final) static members as well.
  • Anonymous classes have the access to the members of the enclosing scope! For example, if we create an anonymous class in a method and in that method there’s a local variable named “fullName”, then in the body of the anonymous class we can access that variable.
Facebook
Twitter
Pinterest
LinkedIn

Top Technologies