• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

anonymous classes

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
preeti
Yes you are right.
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?.
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
G Nadeem
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
G Nadeem
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Roger..
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic