Can we create an anonymous inner class from a concrete class?


There is something surprising in the following code. The ListCell class is not abstract, but we were able to create an anonymous class from it and override its method! How is this possible?

ListCell<TodoItem> listCell = new ListCell<>() {
    public void updateIndex(int i) {

>Solution :

The concept of anonymous classes (cf. JLS, §15.9.5) is simply not related/restricted to abstract classes.

new ListCell<>() {} creates an instance of a subclass (with a synthetic name) of ListCell. Subclassing is possible unless the parent class is final. Subclasses can override methods from the parent class unless they’re final.

Leave a ReplyCancel reply