-4

While a fundamental concept, I don't understand the statement "every CLASS in Java is a subclass of the class object," which is often quoted in JAVA tutorials usually in the inheritance section.

I thought this statement would be true if stated in reverse: every class OBJECT is a subclass of a class." Here is why: in OO design, we use our class template to stamp out our designated objects, so that would make the object subordinate to the class because we create the class before creating our objects.

Yet, since this is a often a quoted gold-standard statement in Object-Oriented design, I know my logic is incorrect. But why?

  • It is incorrect by definition. You are conflating an intensional statement about types, the X class type is derived from the Y class type, with an extensional statement about instances, x, y, z are instances of the class X. Look up type theory, it will be rough going but it will become clear why this is. – Kain0_0 Jan 10 at 22:52
5

These statements are confusing not because they are wrong, but because it is unclear whether the word “class” and “object” refer to the general concept, the Java language concept, or to a specific part of the Java namespace.

By expanding that statement a bit, it might become more clear:

Every Java class is a subclass of the builtin class java.lang.Object.

This statement is true and explains that Java's inheritance hierarchy forms a tree with java.lang.Object at its root. Only primitive types like int are not part of this hierarchy.

We can also phrase that statement from the perspective of an object:

Every Java object is instanceof java.lang.Object.

Your statement merely describes the general relationships of objects and their classes:

Every object is instanceof some class.

That is true in Java, but not all OOP systems use classes (e.g. JavaScript).

  • great explanation. – Minimalist Jan 10 at 20:56
2

In Java there is a built-in class called Object (java.lang.Object).

  • Every class you create in your code is a subclass of the built-in class called Object.

  • This means that every class you create inherits from Object and has methods such as

    equals, compare, toString, hashCode

These methods are implemented for you by Java's built-in Object class.

This is why you can call toString() (for example) on any object in Java and get a String result, even if the object's own class provides no implementation of toString().

See Java 8 Class Object

1

If you write

class Foo {}

in Java, then Foo will implicitly extend Object just as if you had written

class Foo extends Object {}

This is specified in the Java Language Specification, although I must admit I didn't find a better reference than a non-normative example.

So, any class that does not explicitly specify a direct superclass is implicitly a direct subclass of Object. Obviously, any class that explicitly specifies Object as a direct superclass is a direct subclass of Object.

So, what about classes that specify some other class as the direct superclass? For example:

class Bar extends Foo {}

Bar is obviously a direct subclass of Foo. However, since Foo in turn is a direct subclass of Object, this makes Bar transitively an indirect subclass of Object.

And, if you think about it a bit: this is true for every single class in Java. Either it is a direct subclass of Object. Or it is a direct subclass of a direct subclass of Object. Or it is a direct subclass of a direct subclass of a direct subclass of Object. And so on … (Note that the Java Language Specification explicitly defines the subclass relationship to mean the transitive closure of direct subclass relationships.)

Every class is either directly or indirectly a subclass of Object. Or, in other words: every class (except Object) is a subclass of Object. And Object is the only class that doesn't have a direct superclass.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.