wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes InnerClass tell me the ans Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "InnerClass tell me the ans" Watch "InnerClass tell me the ans" New topic
Author

InnerClass tell me the ans

Dhanesh Kumar
Greenhorn

Joined: May 06, 2005
Posts: 17
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.
Bilal Sallakh
Ranch Hand

Joined: Feb 26, 2005
Posts: 45
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


SCJP for J2SE 5.0 (beta)<br />SCJA (beta)
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

"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 ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
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 ]

Tony Morris
Java Q&A (FAQ, Trivia)
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Sep 24, 2003
Posts: 1608
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

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Sep 24, 2003
Posts: 1608
Are you saying that 'implicitly final' and 'implicity unsubtypable' mean exactly the same thing? If so, I strongly refute the statement.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Sep 24, 2003
Posts: 1608
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.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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?


"I'm not back." - Bill Harding, Twister
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608

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

Joined: Sep 24, 2003
Posts: 1608
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
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: InnerClass tell me the ans