You are on page 1of 5

KEERTHANA TECHNOLOGIES

8050673080

Anonymous Inner Class


Anonymous inner class is a class it has no name. It is a non-static nested class without name.
Anonymous inner class makes our code more concise. They enable us to declare and instantiate
the class at the same time.
The class instance is being created at the time of its creation.

Why we need Anonymous inner class?


Suppose there is a requirement that the super class to be re-used with little modification
to the methodOne ().For this we have to create a subclass to superclass and override
the methodOne () method.
For the above requirement we have to implement the sub class.
For the subclass we have to instantiate that class to call the overridden method methodOne()
package Notes;
public class Superclass {
void methodOne(){

class Subclass extends


Superclass {
@Override
void methodOne(){

System.out.println("From
superclass");
}
void methodTwo(){

System.out.println("From
subclass");
}
}

class Innerclass {
public static void
main(String[] args) {
Subclass s1 = new
Subclass();
s1.methodOne();
}
}

System.out.println("From
superclass");
}
}

The above method implementation is little bit lengthy. There is one more method that takes few
lines to satisfy the above requirement , that is Anonymous inner class.
Class Subclass {
public static void main(String[] args) {
Superclass superclass = new
Superclass(){
void methodOne(){
System.out.println("From anonymous inner
class");

Here we just to create an object


reference variable to Superclass and
override the method which needs
modification in the curly brackets
and end with semicolon. There is no
need to create an object to subclass
separately.

}};
superclass.methodOne();
}
}

KEERTHANA TECHNOLOGIES
Class Subclass {
public static void main(String[] args) {
Superclass firstobject = new Superclass(){
void methodOne(){
System.out.println("From AIC first object");
}};
Superclass secondobject = new Superclass(){
voidmethodTwo(){
System.out.println("From AIC second object");
}
};
firstobject.methodOne();
secondobject.methodTcwo();
}
}

8050673080
o It is possible to create only one object
to anonymous inner class, if we want
to create another object the whole
class is to be write again. WHY?
o When we are creating an anonymous
inner class ,it is actually creation of a
subclass to a class to which needs a
modification. This subclass has no
name and it is created in another class.
Thats why the name is Anonymous
inner class.
o While creating an object to AIC we are
also creating an object to subclass with
superclass reference variable. This is a
polymorphism because the superclass
reference is used to refer the both
super class and subclass objects.

Anonymous inner class can be generated by


1. Class (Abstract class also)
2. Interface

Program of anonymous inner class by Abstract class


package Notes;
abstract class Person {
abstract void eat();
}
public class Emp {
public static void main(String[] args) {
Person p1 = new Person() {
void eat(){
System.out.println("nice fruits");
}};
}

p1.eat();

}
Output : nice fruits

KEERTHANA TECHNOLOGIES

8050673080

Explanation:
Person p = new Person () {
void eat () {
System.out.println (nice fruits) ;}};
Here the class is created but the name is decided by the compiler. This class
extends Person class and provide implementation to the eat() method.
An object of anonymous class is created, it is referred by the
reference variable p of super class Person. Parent class reference variable can
refer the object of child class.
If we want to create an anonymous inner class by implicitly extending abstract
class methods, we have to override all the methods of abstract class
otherwise there will be a compile time error.
The internal code generated by the compiler for anonymous inner class
package Notes;
import java.io.PrintStream;
static class Emp$1 extends Person
Emp$1(){}
void eat(){
System.out.println("nice
fruits");}}

Program of Anonymous inner class by interface


package Notes;
interface Eatable{
void eat();
}
public class Emp1 {
public static void main(String[] args) {
Eatable e = new Eatable() {
public void eat(){
System.out.println("nice fruits");
}
Created
};
}

e.eat();

}
Output : nice fruits

KEERTHANA TECHNOLOGIES

8050673080

Explanation :
Here the class is created but the name of the class is decided by the compiler
which implements the eatable interface and provides implementation to the eat()
method.
An object is created , it is referred by the reference variable e of super class Eatable.
The internal code is generated by the compiler for anonymous inner class created by
an interface
package Notes;
import java.io.PrintStream;
static class Emp$2 extends Eatable {
Emp$2(){}
void eat(){
System.out.println("nice fruits");
}
}

We can declare the following in Anonymous inner class


Fields
Extra methods(excluding superclass methods)
IIB
Local Classes
package anonymous;
public class X {
public static void main(String[] args) {
A a1 = new A(){
int x = 10;
void m1(){
System.out.println("m1()");
}
void m2(){
System.out.println("m2()");
}
{
System.out.println("IIB");
}
class X1{
void disp(){
System.out.println("class X1");
}

}}

};

KEERTHANA TECHNOLOGIES

8050673080

Anonymous class has some Restrictions


It is not possible to declare SIB and static members.
If we want to declare static variables, they must be constant variables.
package anonymous;
public class Y {
public static void main(String[] args) {
B b1 = new B(){
static int x = 30;//CTE
static {
System.out.println("SIB");//CTE
}
}}
package anonymous;
public class Z {
public static void main(String[] args) {
A a1 = new A(){
void method1(){
System.out.println("method1()");
}
void disp(){
System.out.println("disp()");
}};

}}}

a1.method1();
a1.disp();
//CTE

In the above program compilation error because the method a1.disp() is not defined
in class A ,it is defined in anonymous class.

You might also like