Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

Java – Anonymous Inner Classes

An inner class without a name is known as anonymous inner class.

Types of anonymous inner classes
i) Anonymous inner class that extends an abstract or concrete class.
ii) Anonymous inner class that implements an interface.
iii) Anonymous inner class as argument.

Anonymous Inner Class that extends an Abstract or Concrete Class

Java Programming Language Anonymous Inner Class

Here new keyword is used to create an object of the anonymous inner class that has a reference of parent class type.

Example

Java Programming Language Anonymous Inner Class Example

Here anonymous inner class creates an object by extending a class Car.
An object of the anonymous class is referred by reference of type Car.

Example of anonymous class and its working

class Car 
{
    public void engineType() 
    {
        System.out.println("Turbo Engine");
    }
}

class AnonymousClassDemo 
{
    public static void main(String args[]) 
    {
        Car c1 = new Car();
        c1.engineType();
		
        Car c2 = new Car()
        {
            @Override
            public void engineType()
            {
                System.out.println("V2 Engine");
            }
        };
        c2.engineType();
    }
}

Output

Turbo Engine
V2 Engine

For us, the anonymous class does not have a name, but Java internally assigns a name for each anonymous class for its working.

For example, the name assigned by Java for anonymous class present in above program is AnonymousClassDemo$1 (because defined in class AnonymousClassDemo).

Let’s see what names are assigned by Java for anonymous classes defined below:

class Car 
{

}

class AnonymousClassDemo 
{
    public static void main(String args[])
    {
        Car c1 = new Car()
        {
			
        };
		
        Car c2 = new Car()
        {
	 		
        };
		
        System.out.println(c1.getClass().getName());
        System.out.println(c2.getClass().getName());
    }
}

Output

AnonymousClassDemo$1
AnonymousClassDemo$2

Anonymous Class that implements an Interface

Java Programming Language Anonymous Class that implements an Interface

Here new keyword is used to create an object of the anonymous inner class that has a reference of an interface type.
Here anonymous inner class creates an object by implementing an interface.

Example

interface Software 
{
    public void develop();
}

class AnonymousClassDemo2 
{
    public static void main(String args[])
    {
        Software s = new Software() 
        {	
            @Override
            public void develop() 
            {
                System.out.println("Software Developed in Java");
            }
        };
        s.develop();
        System.out.println(s.getClass().getName());
    }
}

Output

Software Developed in Java
AnonymousClassDemo2$1

Anonymous Inner class as an Argument

We can use the anonymous Inner class as an argument so that it can be passed to methods or constructors.

Example

abstract class Engine 
{
    public abstract void engineType();
}

class Vehicle 
{
    public void transport(Engine e)
    {
        e.engineType();
    }
}

class AnonymousInnerClassDemo3 
{
    public static void main(String args[])
    {
        Vehicle v = new Vehicle();
        v.transport(new Engine()
        {
            @Override
            public void engineType() 
            {
                System.out.println("Turbo Engine");	
            }
        });		
    }
}

Output

Turbo Engine

 

Leave a reply