Professional Documents
Culture Documents
8050673080
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");
}};
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.
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");}}
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");
}
}
}}
};
KEERTHANA TECHNOLOGIES
8050673080
}}}
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.