Anonymous means no name, a class with no name called as Anonymous class. But is has advantage like to create an instance of class in one step.
We can'e create an object of this class since it has no name.
An anonymous class can implement interface or can extend another class in it like this :
public interface MyInterface extends SuperInterface{
public void value();
}
another super interface to show how can anonymous inner class can implement another interface
public interface SuperInterface {
public void myName();
}
main class to use anonymous class :
MyInterface myInterface = new MyInterface() {
@Override
public void value() {
System.out.println("annonymous inner class using interface");
}
@Override
public void myName() {
System.out.println("annonymous inner class using super interface");
}
};
myInterface.value();
myInterface.myName();
}
output :
annonymous inner class using interface
annonymous inner class using super interface
0 Comment(s)