Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading