What is this type of method overriding called in Java?

That’s an anonymous inner class.

In the example above instead of creating a private class that extends Button we create an subclass of Button and provide the implementation of the overridden method in line with the rest of the code.

As this new class is created on the fly it has no name, hence anonymous. As it’s defined inside another class it’s an anonymous inner class.

It can be a very handy shortcut, especially for Listener classes, but it can make your code hard to follow if you get carried away and the in line method definitions get too long.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)