3.3) Anonymous inner class

As the name implies, an anonymous inner class isn’t defined using an explicit name. An anonymous inner class is created when you combine instance creation with inheriting a class or implementing an interface. Anonymous classes come in handy when you wish to override methods for only a particular instance. They save you from defining new classes. The anonymous class might override none, few, or all methods of the inherited class. It must implement all methods of an implemented interface. The newly created object can be assigned to any type of variable—static variable, instance variable, local variable, method parameter, or returned from a method. Let’s start with an example of an anonymous inner class that extends a class.

ANONYMOUS INNER CLASS THAT EXTENDS A CLASS:

package com.gs.corejava.innerclasses;

class Pen {
	public void write() {
		System.out.println("Pen - write");
	}
}

class Montex extends Pen {
	public void write() {
		System.out.println("Montex - write");
	}

	public void price() {
		System.out.println("Only I can decide");
	}
}

public class Lecture {
	public static void main(String[] args) {
		Pen pen = new Montex();
		pen.write();
	}

}

Let’s replace this usual object instantiation by overriding method write, while instantiating Pen. To override method write() for this particular instance, we insert a class definition between () and ; (() and ; are in bold):

package com.gs.corejava.innerclasses;

class Pen {
	public void write() {
		System.out.println("Pen - write");
	}
}

public class Lecture {
	public static void main(String[] args) {
		Pen pen = new Pen() {
			public void write() {
				System.out.println("Montex - write");
			}

			public void price() {
				System.out.println("Only I can decide");
			}
		};
		pen.write();
	}

}

Example :

package com.gs.corejava.innerclasses;

class Pen {
	public void write() {
		System.out.println("Pen - write");
	}
}

public class Student {
	void attendLecture(Pen pen) {
		Lecture lecture = new Lecture();
		lecture.notes(pen);
	}

	public static void main(String[] args) {
		Student student = new Student();
		student.attendLecture(new Pen() {
			public void write() {
				System.out.println("I am writing");
			}
		});
	}
}

class Lecture {
	void notes(Pen pen) {
		pen.write();
	}
}

ANONYMOUS INNER CLASS THAT IMPLEMENTS AN INTERFACE:

Until now, you should have read that you can’t instantiate interfaces; you can’t use the keyword new with an interface. Think again. Examine the following code, in which the class BirdSanctuary instantiates the interface Flyable by using the keyword new:

Don’t worry; you’ve been reading correctly that you can’t use the operator new with an interface. The catch in the preceding code is that bird refers to an object of an anon- ymous inner class, which implements the interface Flyable.

The anonymous class used to instantiate an interface in the preceding code saved you from creating a class beforehand, which implemented the interface Flyable.

An anonymous inner class can extend at most one class or implement one interface. Unlike other classes, an anonymous class can neither implement multiple interfaces, nor extend a class and implement an interface together.

HOW TO ACCESS ADDITIONAL MEMBERS IN ANONYMOUS CLASSES:

By using an anonymous class, you can override the methods from its base class or implement the methods of an interface. You can also define new methods and vari- ables in an anonymous class (in bold):

You can’t call the additional member, method hungry(), using the reference variable bird. Why? The type of the reference variable bird is Flyable. So the variable bird can access only the members defined in interface Flyable. The variable bird can’t access additional methods and variables that are defined in anonymous classes that implement it.

ANONYMOUS CLASS DEFINED WITHIN A METHOD :

When an anonymous inner class is defined within a method, it can access only the final variables of the method in which it’s defined. This is to prevent reassignment of the variable values by the inner class. Examine the following example.

The code at 1 will compile if ingredient is modified to be defined as a final local variable.

Last updated