interface Int {
public void show();
}
public class Test {
public static void main(String[] args) {
Int t1 = new Int() {
public void show() {
System.out.println("message");
}
};
t1.show();
}
}
You're defining an anonymous class that implements the interface Int
, and immediately creating an object of type thatAnonymousClassYouJustMade
.
This notation is shorthand for
Int t1 = new MyIntClass();
// Plus this class declaration added to class Test
private static class MyIntClass implements Int
public void show() {
System.out.println("message");
}
}
So in the end you're creating an instance of a concrete class, whose behavior you defined inline.
You can do this with abstract classes too, by providing implementations for all the abstract methods inline.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With