Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2006

Tip: Looking for answers? Try searching our database.

Does Object extend Object?

Thread view: 
william.lichtenberger@gmail.com - 05 Jul 2006 20:11 GMT
The javadoc on the class Object is as follows:

 Class Object is the root of the class hierarchy.
 Every class has Object as a superclass. All objects,
 including arrays, implement the methods of this class.

Does this mean that Object implicitly extends Object?

It passes all the inheritance tests such as implementing the methods of
the parent class, the "is a" relationship etc.

So does it in fact extend itself or should it say "Every OTHER class
has Object as a superclass?

Thanks for your thoughts!
Chris Smith - 05 Jul 2006 20:33 GMT
> The javadoc on the class Object is as follows:
>
[quoted text clipped - 3 lines]
>
> Does this mean that Object implicitly extends Object?

Yes, technically, Object extends Object.  What does this mean?  Really
not much, except that it sometimes simplifies reflection code... i.e.

   Class<?> c = ...;
   while (c.getSuperClass() != Object.class) { ... }

works even if c is Object.class (for the most obvious intended
behaviors).

Signature

Chris Smith - Lead Software Developer / Technical Trainer
MindIQ Corporation

william.lichtenberger@gmail.com - 05 Jul 2006 21:02 GMT
This post led me to an obvious conclusion of just trying it...

The following code:

       Object o = new Object();
       Class c = o.getClass();
       Class c2 = c.getSuperclass();
       System.err.println(c.getName());
       System.err.println(c2.getName());

yields:

java.lang.Object
Exception in thread "main" java.lang.NullPointerException
    at main(Main.java:15)

So apparently Object does not in fact extend Object... and the javadoc
is inaccurate.
Thomas Hawtin - 05 Jul 2006 21:11 GMT
> Yes, technically, Object extends Object.  What does this mean?  Really
> not much, except that it sometimes simplifies reflection code... i.e.

JLS3 8.1.4 p184 "... the class Object ... has no direct superclass."

Ugly API wart...

As an experiment I tried to define Object as package java.lang; public
class Object extends java.lang.Object { ... }. javac complains about
cyclic inheritance.

>     Class<?> c = ...;
>     while (c.getSuperClass() != Object.class) { ... }
               ^getSuperclass

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Chris Smith - 05 Jul 2006 21:19 GMT
> > Yes, technically, Object extends Object.  What does this mean?  Really
> > not much, except that it sometimes simplifies reflection code... i.e.
>
> JLS3 8.1.4 p184 "... the class Object ... has no direct superclass."
>
> Ugly API wart...

Whoops!  I was sure I saw that somewhere.  Apparently not.

Signature

Chris Smith - Lead Software Developer / Technical Trainer
MindIQ Corporation

Alan Meyer - 06 Jul 2006 03:54 GMT
> ...
> So does it in fact extend itself or should it say "Every OTHER class
> has Object as a superclass?
> ...

I would think that having a class extend itself doesn't make
a lot of sense, and would involve some serious problems.

First of all, the cyclic inheritance restriction comes into play
and forbids this.

If that were somehow overridden, then there would be an
infinite regress in any method that invoked super().

Then there's the problem of duplication of data.  If field X
is defined in Object, is it also defined in the parent of Object?
And in the parent of the parent, etc.?

All of the above really reduce to one objection, not three,
but they illustrate the multiple facets of the contradiction
involved in self-inheritance.

   Alan
Jeffrey Schwab - 07 Jul 2006 17:19 GMT
>> ...
>> So does it in fact extend itself or should it say "Every OTHER class
[quoted text clipped - 3 lines]
> I would think that having a class extend itself doesn't make
> a lot of sense, and would involve some serious problems.

Back in CS101, I seem to recall being taught that the parent node of any
tree was the node itself.  I guess if Object doesn't extend Object, then
the Java class hierarchy isn't really a tree...

> First of all, the cyclic inheritance restriction comes into play
> and forbids this.
>
> If that were somehow overridden, then there would be an
> infinite regress in any method that invoked super().

No, just methods that don't check for the terminal case of Object.  Even
without Object extending Object, the regression has to check for a base
case.  The regression or while-loop that invokes super() has to know
when to end, anyway.

> Then there's the problem of duplication of data.  If field X
> is defined in Object, is it also defined in the parent of Object?
> And in the parent of the parent, etc.?

Object doesn't have any public fields, so I'm not sure that's an issue.

> All of the above really reduce to one objection, not three,
> but they illustrate the multiple facets of the contradiction
> involved in self-inheritance.

Ruby has a single root Object class, and its superclass() method returns
the equivalent of null.  I wonder how Python handles this.
Oliver Wong - 07 Jul 2006 21:33 GMT
> Back in CS101, I seem to recall being taught that the parent node of any
> tree was the node itself.  I guess if Object doesn't extend Object, then
> the Java class hierarchy isn't really a tree...

   I think, for convenience in writting recursive code, it's useful to
treat any node within a tree as a tree as well. But when we need the
precision (such as now), I think it's useful to distinguish between the tree
and the nodes that make up the tree. If a tree has a parent, that parent
would be a forest, not a tree, nor a node.

   In most contexts, a tree is nothing more than a connected acyclic graph.
Under this definition, the Java class hierarchy is a tree; if you include
interfaces, it is no longer a tree. Taken literally, if any node is
connected to itself, that's a cycle, and thus not a tree.

   - Oliver
Jeffrey Schwab - 07 Jul 2006 22:34 GMT
>> Back in CS101, I seem to recall being taught that the parent node of
>> any tree was the node itself.  I guess if Object doesn't extend
[quoted text clipped - 10 lines]
> include interfaces, it is no longer a tree. Taken literally, if any node
> is connected to itself, that's a cycle, and thus not a tree.

Excellent point. :)
Jochen Schulz - 09 Jul 2006 17:17 GMT
* Jeffrey Schwab:

> Ruby has a single root Object class, and its superclass() method returns
> the equivalent of null.  I wonder how Python handles this.

Python's class <type 'object'> has no bases either:

>>> object
<type 'object'>
>>> object.__bases__
()

(object.__bases__ is an empty list since Python allows multiple
inheritance but object doesn't have a base.)

But the root of Python's inheritance hierarchy is quite interesting.
Since classes are objects, too, <type 'object'> is an instance of <type
'type'>, the mother of all types. At the same time, <type 'type'> is an
instance if itself and derives from <type 'object'>. %-)

Good resource: <http://www.cafepy.com/article/python_types_and_objects/>

J.
Signature

Thy lyrics in pop songs seem to describe my life uncannily accurately.
[Agree]   [Disagree]
                <http://www.slowlydownward.com/NODATA/data_enter2.html>

Mike Schilling - 06 Jul 2006 07:58 GMT
> The javadoc on the class Object is as follows:
>
[quoted text clipped - 3 lines]
>
> Does this mean that Object implicitly extends Object?

No, just sloppy wording.  "A superclass" is odd too; generally, a class
(other than Object itself) has a single superclass, which might, of course,
not be Object.  Better would be "Every class other than Object itself has
Object as an ancestor."

Note that Object is also the superclass of all interfaces.  You an view this
fact as the reason that all methods defined by Object can be invoked on
interface-typed references.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.