Java Anonymous Classes

Can we create objects of Interfaces in Java?

Introduction to Anonymous Classes

Sometimes in programming specially in Java and Android we came across different Interfaces which we must implement in order to use them. We cannot directly create an object of an interface in java. We will definitely need to create a class which implements the interface in order to make its object. But what if we want to implement and use the interface only once throughout our code?. Here where java's anonymous classes comes in.

Anonymous Class is an inner class without a name. Only single instance of that class is created. An anonymous inner class can be useful when making an instance of an object with certain “extras” such as overloading methods of a class or interface, without having to actually subclass a class. Anonymous inner classes are useful in writing implementation classes for listener interfaces in graphics programming.

Notable Properties of Anonymous Classes:

  1. Anonymous class can only implement one interface.
  2. Anonymous Inner class can extend a class or can implement an interface but not both at a time.
  3. Only single object of that class can be created.
  4. Anonymous classes cant have their own constructors.

Anonymous Class implementing Interface

interface MyInterface{
    public int num=17;
    public void onRun();
}
public class MainClass{
    public static void main(String[] args)
    {
        MyInterface interfaceObj = new MyInterface(){
            //Anonymous Class
            @Override
            public void onRun(){
                System.out.println("Number is : "+num);
            }
        };
        interfaceObj.onRun();
    }
}

This code will output the following:

Number is : 17

Here we created an Interface named MyInterface. It has one data member and one member function declared. Then in the main method we are creating an object with Interface type and then writing a block for class where we are overriding the onRun() method. This block is known as anonymous class. Object of this class will be created at the runtime and will be asigned to the variable of Interface type.

Anonymous Class for extending other class.

Anonymous Classes can also be used to extend other classes in java. For example look at the below code.

class Base{
    public void printMessage()
    {
        System.out.println("This method is called from Base Class");
    }
}
public class MainClass{
    public static void main(String[] args)
    {
        Base obj = new Base(){
            //Anonymous Class
            @Override
            public void printMessage(){
                System.out.println("This method is called from Anonymous Class");
            }
        };
        obj.printMessage();
    }
}

Output of the above code will be

This method is called from Anonymous Class

Conclusion

Hence we can use anonymous Class whenever we need a single instance of an interface or we want to extend a base class for single time use.