18

I'm fairly new to programming but I've taken the Intro CS class at my school, so I understand most of the basics (or thought I did). I'm trying to teach myself some OpenGL via JOGL and I came across a few lines of code that I couldn't understand. Am I missing something?

frame.addWindowListener(new WindowAdapter() {
  public void windowClosing(WindowEvent e) {
    System.exit(0);
  }
});
  • I checked the Javadoc, and WindowAdapter is an abstract class. So how can he be instantiating it?

  • Or is this even creating an instance?

  • It almost looks like the code extends WindowAdapter or overrides the windowClosing method, but how is that possible without writing a new class?

GEOCHET
  • 21,119
  • 15
  • 74
  • 98
AdamJames
  • 367
  • 2
  • 11
  • 7
    Can the downvoters comment? Seems like a legit question to me.. – Mike Christensen Jul 09 '13 at 05:11
  • 1
    @MikeChristensen totally agree, downvoters must have a good reason, but they should at least explain why so the question can be improved – morgano Jul 09 '13 at 05:13
  • 8
    I don't really understand the close votes. To me this is a valid, well articulated question. – NPE Jul 09 '13 at 05:13
  • Sorry I'm a bit new to this. How exactly do I do that? – AdamJames Jul 09 '13 at 05:28
  • Ah never mind figured it out. Checkmark! – AdamJames Jul 09 '13 at 05:29
  • 1
    Similar: [What is this syntax called? `new Type() { ... }`](http://stackoverflow.com/q/8239932/486504) (disclaimer: my own question :)) – user Jul 09 '13 at 08:47
  • 2
    Note that [**System.exit() terminates the JVM**](http://docs.oracle.com/javase/6/docs/api/java/lang/System.html#exit%28int%29) and that [there are other ways to exit](http://www.javapractices.com/topic/TopicAction.do?Id=86). The behavior of System.exit() may or may not be what you want. – user Jul 09 '13 at 08:49
  • 1
    I really don't understand the off-topic closure reason. This question is a well-posed and good question, which is a duplicate of Michael's question. – Bakuriu Jul 09 '13 at 09:59
  • I don't understand either. It wasn't a question about code I wrote; the code was from an example and I was trying to find out what it did. The answers were very helpful. Is that not a valid question for Stack Overflow? Also, does having a question closed affect reputation? – AdamJames Jul 09 '13 at 11:22
  • @Bakuriu I'm not sure I'd call mine a duplicate of this, actually. Sure, it is about the same syntax, but when asking mine I knew what it did and was looking for the name. This question is about what the code does. Hence similar, but not proposed as a duplicate. – user Jul 10 '13 at 07:18
  • @AdamJames Personally, I think it's a valid question, and there are currently no active close votes on it. Having questions closed does not affect your reputation, although *possibly* (I don't know) having a *large* number of closed questions may affect your ability to post new questions. That's a matter for Meta, though. The only things that affect reputation is up- and downvotes on questions and answers, and actions which affect your questions and answers (deletions, user account removals etc.). – user Jul 10 '13 at 07:20
  • @MichaelKjörling Nevertheless the answer to the other question would be a good answer for this question, and this is what you should look at when voting to close as duplicate. – Bakuriu Jul 10 '13 at 07:43
  • @AdamJames Correction, some other things also do affect reputation. Proposing edits which are approved, for example, earns you a small amount of reputation. Still, the main point is the same: having a question closed does not affect your reputation directly. – user Jul 10 '13 at 07:47
  • Guys, I reedited this question to be more clear. Please review it and revert if you thinks it's no good. – Mike Szyndel Jul 31 '13 at 08:32

2 Answers2

20

It almost looks like the code extends WindowAdapter or overrides the windowClosing method

This is exactly what's happening.

but how is that possible without writing a new class?

In fact, the code is creating a new (anonymous) class. It's just that the syntax is different to what you've come across so far. Take a look at the tutorial.

For a discussion of how anonymous classes are used, see How are Anonymous (inner) classes used in Java?

Community
  • 1
  • 1
NPE
  • 486,780
  • 108
  • 951
  • 1,012
8

The Concept used is Anonymous class !! .... Since WindowAdapter is an abstract class you cannot make it's object it but using Anonymous Class concept you can call its constructor or use the functions without assigning it to an object of it's type ..

Another way of using Abstract classes data variables and methods is to make objects of it's derived classes

In this way u can have a WindowAdpater instance passed in the parameter without any error.

Vaido
  • 111
  • 1
  • 7