user705414
user705414

Reputation: 21220

What's this language feature called?

JPanel panel = new JPanel() {
        public void setBackground(Color c) {
           Logger.global.info("setBackground: c=" + c);
           super.setBackground(c);
        }
    };

I only know I can do JPanel panel = new JPanel();

Why can someone do the above? What's the name for it?

Upvotes: 3

Views: 114

Answers (2)

mishadoff
mishadoff

Reputation: 10789

It is an anonymous class that you extend and define additional methods or override existing ones.

Note that the same way you can extend interfaces or abstract classes.

Upvotes: 0

aioobe
aioobe

Reputation: 421310

It is called an anonymous class.

That code basically creates a subclass of JPanel "on the fly" without giving it a name (thus the term anonymous class) and instantiates it.

Related questions and links:

Upvotes: 7

Related Questions