• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

InnerClass tell me the ans

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hich of the follow are true statements.

a. An anonymous class can extend only the Object class.
b. An anonymous class can not implement an interface.
c. An anonymous class can be abstract.
d. An anonymous class is implicitly final.
e. An anonymous class can be static.
f. The class instance creation expression for an anonymous class must never include parameters.
g. An anonymous class must declare at least one constructor.
h. None of the above.
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Dhanesh Kumar:
hich of the follow are true statements.

a. An anonymous class can extend only the Object class.
b. An anonymous class can not implement an interface.
c. An anonymous class can be abstract.
d. An anonymous class is implicitly final.
e. An anonymous class can be static.
f. The class instance creation expression for an anonymous class must never include parameters.
g. An anonymous class must declare at least one constructor.
h. None of the above.



Look at this:

You can see that:

a, b are clearly false
c is logically false

e: is true since the above can be in a static method (without an enclosing class so the Outer.this is unavailable)
as for f, anonymous classes don't have a constructor, but if the class/interface they extend has a constructor that accepts parameter you may pass these paramter at creation: new SuperClass(p1, p2){...}

g: false since it can't have anyone!

d: I think so but it's better if you check the JLS

Regards,

Bilal
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"An anonymous class is always an inner class (�8.1.2); it is never static (�8.1.1, �8.5.2). An anonymous class is always implicitly final (�8.1.1.2)."

Ref: http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#252986

It's true that an anonymous class can be instantiated within a static method, but the anonymous class is not a member of the enclosing class, and "the access modifier static pertains only to member classes (�8.5, �9.5)."

Ref: http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#21613

"An inner class is a nested class that is not explicitly or implicitly declared static."

Ref: http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#262890

So "d" is the only correct answer.
[ May 12, 2005: Message edited by: marc weber ]
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An anonymous class is not implicitly final.



It is however, implicitly not 'subtypable'. The same can be said for many constructs. For example, a top level class with all private constructors is also implicitly not 'subtypable', however it may not be final (though good form mandates that it is).
[ May 12, 2005: Message edited by: Tony Morris ]
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tony Morris:
An anonymous class is not implicitly final...


Are you advising that candidates contradict the JLS when taking the SCJP exam?
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a) Where is the contradiction in the JLS (your reference to 8.1.1.2 says nothing regarding the topic)?
b) If such a contradiction did exist, the question is, "should candidates use the reference implementation or the JLS as an authoritative source?" to which I would answer "the reference implementation" for obvious (hopefully) reasons.
c) I was not providing advice, merely facts with proof - how one extrapolates from that is up to the individual.
b) My advice on how to interpret the questions in the exam is grounds for disqualification of my certification (freedom of speech? yeah right) so I will refrain.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, I don't see a contradiction in the JLS (nor do I see a problem with �8.1.1.2).

But I am confused about your test code. It seems to be saying that because the Class object for an anonymous class does not include the ACC_FINAL flag with its Modifiers, it's not implicitly final. But Table 4.1 of the JVM Specs indicates that this flag is for classes that are "declared final," which does not preclude (in my interpretation) the possibility of an implicitly final class. Is there something more to this?
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you saying that 'implicitly final' and 'implicity unsubtypable' mean exactly the same thing? If so, I strongly refute the statement.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tony Morris:
Are you saying that 'implicitly final' and 'implicity unsubtypable' mean exactly the same thing? If so, I strongly refute the statement.


I'm not familiar with the term "implicitly unsubtypable," so I can't say. Could you elaborate on the distinction?
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure.

This class cannot be subtyped. I doubt that there is such word as 'unsubtypable' so I apologise for apparantly making it up. By your definition (if I'm assuming correctly), this class is also final, which is something that is clearly (at least, to me) incorrect.
For example,

will result in a compile-time error.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ummm guys, �8.1.1.2 is irrelevant. The relevent section of the JLS is the one Marc originally quoted (and linked to, but without identifying it by section number): JLS 15.9.5. Section 8.1.1.2 only came up because it was incidentally referenced from within 15.9.5. The relevent section was quite clear: "An anonymous class is always implicitly final." Reflection does seem to offer a contradictory answer. Of course, if you follow the links from JTiger through java.lang.reflect.Modifer you see that isFinal() is actually interpreted in terms of JVMS2 table 4.1, which tells us that ACC_FINAL means "declared final; no subclasses allowed". Well, the latter is certainly true for an anonymous class, but the former is not. It's implicitly final according to the JLS, not declared final.

This would be a perfectly good explanation of the discrepancy, except that when we use reflection to check whether other things are final, we start getting inconsistent answers. In particular the methods of an interface will show up as public, final, and abstract - even if none of these attributes are declared. Of course they're true implicitly, but that's not what table 4.1 said.. The point is, reflection may or may not distinguish between implicit and explicit attributes.

Does it matter? Not really unless we want to argue semantics. (A popular pastime for Tony and me, but not very rewarding for SCJP candidates I think.) The JLS says an anonymous class is final; reflection and the class file say it isn't -- but either way, the end result is that you can't subclass it. Ever. We all agree on that, right? And, since this is the SCJP forum, I don't think the additional subtleties really matter here.

[Tony]: For example, a top level class with all private constructors is also implicitly not 'subtypable',

I've seen you lambaste others for overlooking rule exceptions related to nested classes being able to access private members of the containing class, Tony. Getting sloppy?

While we're at it Tony, you might want to correct the compilation error in your AnonymousClassFinal class above, eh?
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


For example, a top level class with all private constructors is also implicitly not 'subtypable',

I've seen you lambaste others for overlooking rule exceptions related to nested classes being able to access private members of the containing class, Tony. Getting sloppy?


Not at all - top level class.


While we're at it Tony, you might want to correct the compilation error in your AnonymousClassFinal class above, eh?


OK I concede - editing source files in a HTML text field is a bit sloppy.

 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unless you mean, subtyping a top level class with a nested class, to which I concede again.
What can I say!? It's Friday!
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Tony]: OK I concede - editing source files in a HTML text field is a bit sloppy.

Agreed. Sorry, that was a cheap shot on my part, on par with complaining about spelling errors when the meaning is clear.

Unless you mean, subtyping a top level class with a nested class, to which I concede again.

Yep, that was what I meant.

What can I say!? It's Friday!

Good answer. I'm having a beer right now; please have a beer after work and we'll pretend it's simultaneous.
 
reply
    Bookmark Topic Watch Topic
  • New Topic