Re: Interesting Java interview question



Doug Dunn wrote:
I hate the way this news group has gone to hay. It used to be a good, active news group. C# advocates are punk for bothering other programmers in this group. I have prepared www.javaspec.org/wiki/Is_Java_dying%3F in response to their continual nonsense. Please get a life and leave this newsgroup alone.

Have you looked at the numerous subgroups of c.l.java, e.g., c.l.j.programmer? I get ~ 80 - 100 messages/day on that newsgroup, far from dead.

Meanwhile, this appears to me to just be to sets of arbitrary blocks-- the innnermost {{}}-- inside of an anonymous class body. The only question here is this:

Not quite.
new Object() { // Anonymous class declaration
{ // Anonymous class constructor declaration
{ // Arbitrary scope creation
}
}
}

You've declared a constructor as well (albeit one identical to the default constructor).

Anonymous classes implicitly subclass the class in which they are declared.

Where's the question mark, if it's a question?

This is also an incorrect or at best misleading statement. The anonymous class that is declared is a subclass of Object, nothing more, nothing less. Certainly, the output of this snippet does change depending on the class in which it was declared, but the anonymous class does not extend it.

The question is seeking to find out if the reader knows this because in isolation it is not possible to know what will print.

The question, I think, is trying to identify the following aspects of Java knowledge:
1. Anonymous classes, esp.
1a. Constructors of said classes
1b. Name of said class
2. Arbitrary scope creation
3. What Object's toString method does

of which 1a and 3 are probably what the interviewer is most interested in knowing.

--
Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald E. Knuth
.



Relevant Pages

  • Re: Interesting Java interview question
    ... public class InterviewQuestion { ... new Object{// Create a new anonymous class subclassing Object ... Many people know how to create these anonymous classes, the constructor for said classes was brought up in c.l.j.p a while back in some other contexts, and the arbitrary scoping is little-used AFAICT. ...
    (comp.lang.java)
  • Re: Explain this Technique -- new() followed by an anonymous class declaration
    ... > followed by an anonymous class declaration. ... See the "Class Instance Creation Expressions" section of The Java ... of Object that implements the interface" is declared. ... Its limits are those of an anonymous class, such the class not being able ...
    (comp.lang.java.help)
  • Re: Annoymous inner class make public
    ... NOT // typo ... An anonymous class is declared and an ... superclasses constructor via the instantiation statement. ... Self congratulation for a limited time only! ...
    (comp.lang.java.programmer)