Thanks for providing the source of your facts. Now I can go into a bit of detail.
"Head First Java 2nd edition" says on page 208:
Every class in Java extends Object
Class Object is the mother of all classes; it's the superclass of everything.
Further down in the paragraph it says:
Every class you write extends Object, without you ever having to say it. [...]can think of it as though a class you write looks like this: public class Dog
extends Object
{ }
[...] Dog already extends something, Canine. [...] The compiler will make Canine extend Object instead.
While the general idea that they're trying to communicate (that every class in Java has java.lang.Object
as a superclass) is correct, they are using the wrong terminology, and the example using Dog
is plain wrong and that is leading to your confusion.
I've written a Java compiler (for Java 1.4) and I'm pretty familiar with the Java Language Specification, so bear with me.
To prove that the terminology is "Head First Java" is wrong I need to quote from the Java Language Specification, which is a bit technical.
But to start with I can give you an easier explanation how you can see this.
Better definition
Every class in Java that does not extend
another class explicitly, extends Object
When you write:
class Canine { }
Then the compiler takes this to mean:
class Canine extends Object { }
But when you write:
class Dog extends Canine { }
Then the compiler sees that you have already explicitly extended a class, and
doesn't change the meaning of your code.
Since Head First Java second edition is based on Java 5, I'll use the Java Language Specification for that version of Java.
The meaning of extends is defined in section 8.1.4 of the JLS:
8.1.4 Superclasses and Subclasses
The optional extends clause in a normal class declaration specifies
the direct superclass of the current class.
Super:
extends ClassType
As you see, extends only refers to a direct superclass. In the example in Head First Java, Dog doesn't extend Object, because its direct superclass is Canine. Only Canine extends Object.
What they meant to say is that Object is a superclass of all other classes in Java. This is defined in JLS section 4.3.2:
4.3.2 The Class Object
The class Object
is a superclass (§8.1) of all other classes. A
variable of type Object
can hold a reference to the null
reference
or to any object, whether it is an instance of a class or an array
(§10). All class and array types inherit the methods of class
Object
, [...]
I'm afraid that you got sidetracked by the misleading way that this was presented in the 2nd edition of Head First Java.
Hopefully that have already fixed this in newer editions (if anyone has one, please confirm/refute), otherwise we should inform the authors.