freetimestudys@gmail.com          +91-7097633380
  Login Here                        

Anonymous inner class:-

1. The name less inner class is called anonymous inner class.
2. it can be used to provide the implementation of normal class or abstract class or interface

Anonymous inner class program

abstract class ex
{
abstract void m1();
};
class OuterClass
{
void m2()
{
System.out.println("m2 method");
}
ex t=new ex()
{
void m1()
{
System.out.println("annonymus inner class");
}
};
};
class Test
{
public static void main(String[] args)
{
OuterClass o=new OuterClass();
o.m2();
o.t.m1();
}
};

intput and output

output:m2 method
annonymus inner class

Anonymous inner class program by using interface

interface Test
{
abstract public void m1();
};
class OuterClass
{
void m2()
{
System.out.println("m2 method");
}
Test t=new Test()
{
public void m1()
{
System.out.println("annonymus inner class");
}
};
};
class Demo
{
public static void main(String[] args)
{
OuterClass o=new OuterClass();
o.m2();
o.t.m1();
}
};

intput and output

output:m2 method
annonymus inner class