Tuesday, January 5, 2016

ANONYMOUS INNER CLASS


ANONYMOUS INNER CLASS AND INTERFACE


Anonymous classes in java are defined in a different way than that of a normal java classes. In java anonymous classes can be created in either of the two ways.

1) Using a class reference variable.
2) Using an interface.

Sample code:
public class Anonmyoussec
{
public  void Anonmy1()
{
System.out.println("ANONMYOUS CLASS MAIN");
}

}
class Anonmyoussec1
{
static Anonmyoussec anon=new Anonmyoussec()
{
public void Anonmy1()
{
System.out.println("ANONMYOUS CLASS SUB MAIN");
}
};
public static void main(String args[])
{
anon.Anonmy1();
}
}


ANONMYOUS CLASS INTERFACE:


interface Anonmy1
{
public void sum();
}
public class InnerClass
{
static Anonmy1 a1=new Anonmy1() {

@Override
public void sum() {
System.out.println("DATA SUM");
}
};
public static void main(String[] args)
{
a1.sum();
}


}

No comments:

Post a Comment