I can’t remember the name of this concept.
public interface MainInterface {
public void method1();
public void method2();
}
void testMethod() {
methodMain(new MainInterface() {
@Override
public void method1() {
System.out.println("This is method1");
}
@Override
public void method2() {
System.out.println("This is method2");
}
});
}
void methodMain(MainInterface mi) {
mi.method1();
mi.method2();
}
- I create the instance of MainInterface without giving the name of object
- I implement the interface methods
- Then pass the instance with unknown name to methodMain.
What is this concept and how exactly it works?
You have created an instance of an Anonymous Inner Class (i.e. a class without a name).