Does Object extend Object?

  • Thread starter william.lichtenberger
  • Start date
W

william.lichtenberger

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!
 
C

Chris Smith

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?

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).
 
W

william.lichtenberger

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.
 
T

Thomas Hawtin

Chris said:
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
 
C

Chris Smith

Thomas Hawtin said:
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.
 
A

Alan Meyer

...
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
 
M

Mike Schilling

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?

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.
 
J

Jeffrey Schwab

Alan said:
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.
 
O

Oliver Wong

Jeffrey Schwab said:
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
 
J

Jeffrey Schwab

Oliver said:
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.

Excellent point. :)
 
J

Jochen Schulz

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

()

(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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top