| Author |
anonymous classes
|
preeti khane
Ranch Hand
Joined: Mar 12, 2003
Posts: 93
|
|
is the following true or false a anonymous class (always) implicitly extends the Object class... I am tending to say true, just because even if the anon class extended another class, Object is somewhere up in the hierarchy
|
 |
Lawrence Chettiar
Ranch Hand
Joined: Apr 10, 2003
Posts: 62
|
|
preeti Yes you are right.
|
SCJP 1.4 (86%)<br />SCMAD Beta (77%)<br />SCEA (part I:89%,partII:93%)<br />MCAD (cleared 70-315 & 70-320 [Score 980])
|
 |
Anupam Sinha
Ranch Hand
Joined: Apr 13, 2003
Posts: 1088
|
|
|
But what if an anonymous class implemented an interface and an anonymous class can either implement or extend so how does Object come into the hierarchy.
|
 |
G Nadeem
Ranch Hand
Joined: Apr 25, 2003
Posts: 48
|
|
hi, i think, an anonymous class DOES NOT ALWAYS extend Object. it only does so when it implements an interface. return new Runnable()... ; implicitly extends object. when an interface does not extends another interface, it implicitly extends object return new MyClass ()....; extends MyClass (only). As for as the Object being at top of hierrarchy is concerned, we can not say, in technical sence, that all classes extend from object class Child extends Parent(){} Child only extends Parent and not Object, because it can extend only one class). though ANY class IS-A object is valid. further it is worth noting that explicitly stating 'extends Object' for anonymous class in not allowed. if i am wrong, plz correct.
|
 |
Anupam Sinha
Ranch Hand
Joined: Apr 13, 2003
Posts: 1088
|
|
Hi Nadeem I guess that it should be the other way round. When an anonymous class extends and object it extends Object class implicitly because somewhere in the that object's hierarchy the Object class has to be present. But when an Objecty implements and interface it only implements that interface and interface don't inherit from Object. As for your example class Child extends Parent(){} first of all after Parent parentheis() should not be there, secondly if the Parent class will now extend Object if it does not then some of its superclass will extend Object so Object is inherited implicitly.
|
 |
Stan Forest
Greenhorn
Joined: Mar 03, 2003
Posts: 12
|
|
Page 458 of the Java Language Specification states: "The class Object is the single root of the class hierarchy. All objects, including arrays, implement the methods of this class." An anonymous class instantiates an object. So it is an extension of the Object class.
|
 |
G Nadeem
Ranch Hand
Joined: Apr 25, 2003
Posts: 48
|
|
Hi Anupam, thanks for correction. Complete Java 2 Certification, section Anonymous Classes,Page 195..
If u declare a class that implements a single explicit interface, then it is a direct subclass of Object
do we end up with Lawrence's conclusion then?.
|
 |
Rajinder Yadav
Greenhorn
Joined: May 13, 2003
Posts: 27
|
|
an anonymous class does extend the Object class, an anonymous class is simply an un-named class you can try this yourself, create an anonymous class and check it against the Object class using the instanceof operator.
|
When faced with an easy thing to do and a hard thing to do, always pick the right thing to do!<p><a href="http://yadav.shorturl.com" target="_blank" rel="nofollow">Rajinder Yadav</a>
|
 |
G Nadeem
Ranch Hand
Joined: Apr 25, 2003
Posts: 48
|
|
hi Anupam, sorry , didnot mention the book authors. above quote was from Simon Roberts et al. so can anyone finalize what should be the response if asked "an anonymous class (always) implicitly extends the Object class..." yes/no..?? becasue we seem to have different arguments here. thanks in advance.
|
 |
Roger Chung-Wee
Ranch Hand
Joined: Sep 29, 2002
Posts: 1683
|
|
The question is not worded well. Try rewording it like this. "Is an anonymous class always in the Object hierarchy?" The answer is yes, because the class will either inherit the direct superclass or Object (if it implements an interface). What matters is your understanding rather than the answers to some obscurely worded questions.
|
SCJP 1.4, SCWCD 1.3, SCBCD 1.3
|
 |
G Nadeem
Ranch Hand
Joined: Apr 25, 2003
Posts: 48
|
|
thanks Roger..
|
 |
 |
|
|
subject: anonymous classes
|
|
|