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<>() {
@Override
public void updateIndex(int i) {
super.updateIndex(i);
}
};
CodePudding user response:
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.