Learnitweb

Anonymous Inner Class in Java

Anonymous inner class is an inner class without a name.

Anonymous class is commonly used to create implementation classes of listener interfaces. Anonymous class is very useful while creating an instance of subclass(of a class) or implementing an interface without actually extending and with less code.

The main purpose of creating anonymous inner class is to override one or more methods of the super class or to implement methods of an interface with creating a just-in–time and anonymous instance.

There are two ways to create anonymous inner class:

  1. As a subclass of class.
  2. As an implementer of an interface.

1. Anonymous inner class type 1 – instance of Class

Let us first write some code to understand this type of inner class.

class Parent {
	public void print() {
		System.out.println("this message is from parent class");
	}
}

class MyClass {
	Parent p = new Parent() {
		public void print() {
			System.out.println("this message is from anonymous class");
		}
	};
}

1.1 Explanation

  1. Parent is a class. This class has a method print() which does nothing specific and just prints a message.
  2. MyClass is another class which has a member variable p which is of type Parent.
  3. As you can notice there is no semicolon(;) after new Parent() but a curly brace ({). This is syntax of writing an anonymous class.
  4. This code is creating a just-in-time, anonymous, new instance of subclass of Parent class.
  5. There is a semicolon(;) after the closing curly brace (}). This is important – Anonymous inner class’ code ends with a semicolon.

Note: An anonymous, just-in-time, new instance is created of the subclass in this case. Since the new instance is of subclass, polymorphism comes into picture. That is, only those methods can be called on the anonymous inner class reference which are defined in parent class.

1.2 Example

package com.learnitweb;

class Parent {
	public void print() {
		System.out.println("this message is from parent class");
	}
}

class MyClass {
	Parent p = new Parent() {
		public void print() {
			System.out.println("this message is from anonymous class");
		}
	};
	
	public void executeTest() {
		p.print();
	}
	
	public static void main(String args[]) {
		MyClass myclass = new MyClass();
		myclass.executeTest();
	}
}

Output

this message is from anonymous class

2. Anonymous inner class type 2 – implementer of interface

In this type of anonymous inner class, an anonymous implementer of the interface type is created.

interface Printable {
	public abstract void print(); 
}

class MyClass {
	Printable p = new Printable() {
		public void print() {
			System.out.println("this message is from anonymous class");
		}
	};
}

2.1 Explanation

  1. Printable is an interface. This interface has an abstract method print().
  2. MyClass is a class which has a member variable p which is of type Printable.
  3. As you can notice there is no semicolon(;) after new Printable() but a curly brace ({). This is syntax of writing an anonymous class.
  4. This code is creating just-in-time, anonymous, a new implementer of Printable interface.
  5. There is a semi-colon(;) after the closing curly brace (}). This is important – Anonymous inner class’ code ends with a semicolon.

2.2 Example

package com.learnitweb;

interface Printable {
	public abstract void print(); 
}

class MyClass {
	Printable p = new Printable() {
		public void print() {
			System.out.println("this message is from anonymous class");
		}
	};
	
	public void executeTest() {
		p.print();
	}
	
	public static void main(String args[]) {
		MyClass myclass = new MyClass();
		myclass.executeTest();
	}
}

Output

this message is from anonymous class

2.3. Important points

  1. You can not instantiate an interface. While creating an anonymous inner class, you can see use of new with an interface that is new Printable(){ in the above example. This is only possible only in this case. Otherwise you can not use new with the interface.
  2. The implementer in this case can implement only one interface at a time.

3. Define an anonymous inner class as an argument

Yes, you can define an anonymous inner class as an argument. Let us see this with an example.

package com.learnitweb;

interface Printable {
	public abstract void print(); 
}


class Printer {
	public void print(Printable p) {
		p.print();
	}
}
class MyClass {
	public static void main(String args[]) {
		Printer printer = new Printer();
		printer.print(new Printable() {
			public void print() {
				System.out.println("Message from anonymous class");
			}
		});
	}
}

Output

Message from anonymous class

3.1 Explanation

  1. Printable is an interface which has an abstract method print().
  2. Printer is a class which has method print which accepts argument of type Printable.
  3. Myclass has a method main. In this method, printer is an instance of type Printer. While calling print() method, we need to pass an argument of type Printable. So we have created an anonymous implementer of type Printable. Note that we have created this implementer in the argument itself while calling the method.

4. Important point

  • The argument local inner class ends with });
  • The plain anonymous inner class ends with };