Concepts / Subclasses and Inheritance

Subclasses and Inheritance

Object is a special class in package java.lang and is the one class in Java that is not a subclass of any other class.

  • Programming
Interactive lab

Try it: Inheritance and Method Lookup

How a subclass inherits from its parent, how Python finds an attribute by walking the instance, then the class's method resolution order up to object, how overriding and super() work, why the same call does different things on different objects (polymorphism), and what isinstance / issubclass check.

How it works

  1. class Dog(Animal) gives Dog the MRO Dog → Animal → object; Puppy(Dog) gives Puppy → Dog → Animal → object.
  2. obj.name searches obj.__dict__, then each class of type(obj).__mro__ in order; the first class that defines the name wins, so a subclass method overrides the parent's.
  3. super().method() starts the search in the class after the one the current method was defined in, e.g. to run the parent's __init__.
  4. Calling the same method on a list of different objects runs each object's own version (polymorphism).
  5. isinstance(obj, C) is True when C is in type(obj).__mro__; if no class has the name, Python raises AttributeError.

Default run (63 steps): Ready: Animal, Dog, Cat and Puppy, then the statements you picked. Each attribute is searched along the class's MRO. … Finished. Printed: Rex the dog says Woof / Tom the cat says Meow / Bo the dog says Woof! / True / Puppy / Dog / Animal / object.

Simplified: The lab's own small Python interpreter runs a fixed, hand-written script (you choose the variant and the values, never the code); nothing is executed as real Python. Every script was run in CPython 3.12 with the same inputs and the lab reproduces its output and error messages. Single inheritance only (the lab's MRO code is Python's C3 algorithm, but no class here has two parents).

Educational simulation

Loading the simulation…

A Hidden Layer Under Every Class

Most of what you have learned about object-oriented programming so far centers on subclasses. A subclass takes on the properties and behaviors of an existing class, and it can change those inherited behaviors or add new ones of its own. Creating subclasses is a skill you will practice in depth later. This article looks at a side of the same system you may not have noticed yet: every class in Java, with one exception, already sits underneath some other class. The class that makes this possible is named Object.

Object is a special class in the package java.lang. It is the one class in Java that is not a subclass of any other class. Every other class is a subclass of some class, and a class that does not name a parent automatically becomes a subclass of Object. Keep this in mind as you read: the question this article answers is what it means for Object to sit at the top of the class structure, and why that placement matters for the code you write.

Tracing the Chain Upward

Think of Object as the root of the whole class structure. Start at any class you write and follow its parents upward. The chain can be one step long, as with a class that has no explicit parent, or several steps long, as with a class that inherits from a class that itself inherits from another. In every case the chain eventually reaches Object, and Object is the top of the chain. Nothing sits above it.

Objectroot of every chainAnimalno extends clauseDogextends AnimalCarno extends clausesubclass ofsubclass ofsubclass of
Where does Object sit relative to every other class, and how does each class connect upward to it? Animal and Car have no extends clause, so they sit directly under Object. Dog extends Animal, so it sits one level below Animal. Following any arrow upward always reaches Object.
Worked example

Tracing Dog Up to Object

Starting at the class Dog, list every superclass you pass through until you reach the top of the chain.

  1. 1
    Step 1: Start at Dog

    Dog is written with extends Animal, so its first parent is Animal.

  2. 2
    Step 2: Move to Animal

    Animal has no extends clause, so it is not written as a subclass of any named class. Java treats it as a subclass of Object.

  3. 3
    Step 3: Move to Object

    Object is the one class that is not a subclass of any other class. The chain stops here.

Result

Dog -> Animal -> Object. Object is the top of the chain, and every class you write reaches it this way.

Finding the Superclass When No extends Is Written

A class that does not state an extends clause still has a superclass. The rule is simple: a class with no explicit parent automatically becomes a subclass of Object. So when you read a class declaration with no extends keyword, you should not conclude that it has no parent. Its direct superclass is Object.

Animalwritten with no extendsObjectimplicit superclassimplicit superclass
When a class has no extends clause, what does Java treat as its superclass? The class Animal is written with no extends clause, and it is still placed directly under Object in the hierarchy.
java
Notes on the code
  • Animal has no extends clause, so its superclass is Object.
  • Dog extends Animal, so its chain is Dog, Animal, Object.
  • Car also has no extends clause, so it sits directly under Object alongside Animal.

What do you think happens?

In the code above, class Car is declared with no extends clause. What is Car's direct superclass?

  • Animal

  • Dog

  • Object

  • Car has no superclass

Reveal answer
Answer:

Object

A class with no explicit parent automatically becomes a subclass of Object. The absence of an extends clause does not mean there is no superclass.

Why an Object Variable Can Hold Any Object

Because every class is a subclass of Object, a variable whose type is Object can refer to an object of any class you write. The variable does not need to know the specific class in advance. The example below illustrates this relationship. It was written for this article to show the rule in action.

java
Notes on the code
  • Each variable has the declared type Object.
  • Each one refers to an object of a different class you wrote.
  • This works because Dog, Car, and Animal are all subclasses of Object, directly or through a chain.

Methods Every Object Inherits

The second major fact in this section concerns behavior rather than structure. Class Object defines several instance methods. Because every other class is a subclass of Object, every other class inherits those methods. The source states that these methods can be used with any object whatsoever. That phrase matters: you do not need to write any special code in a class for its objects to have access to the methods defined in Object.

Objectdefines instance methodstoStringdefined in ObjectDogsubclass of Animalequalsdefined in ObjectAnimalsubclass of Objectdefinesdefinesparent ofparent of
Why can any object call methods defined in Object, and how are those methods passed down? Object defines the methods, and Dog reaches them by inheriting from Object through Animal. Dog does not need its own copy written in its source code.

The methods named in the diagram, toString and equals, are examples of instance methods that Object defines. The source describes this group in general terms and does not list every method, so treat these two names as illustrations of the idea rather than a complete inventory. What matters is the mechanism: a method defined once in Object is available on every object in the program, including objects of classes you have not yet written.

Mistakes Beginners Make With Object

  • Common mistake

    Assuming a class with no extends clause has no superclass

    What it looks like
    class Car { } is read as a standalone class with no parent.
    Why it fails

    A class with no explicit parent automatically becomes a subclass of Object.

    Do this instead

    Read class Car { } as if it said class Car extends Object.

  • Common mistake

    Believing Object is a subclass of some other class

    What it looks like

    Drawing Object beneath Animal or another class you wrote.

    Why it fails

    Object is the one class in Java that is not a subclass of any other class. It is the root.

    Do this instead

    Draw Object at the top of every hierarchy and every other class beneath it.

  • Common mistake

    Thinking the methods of Object exist only if you write them in your class

    What it looks like

    Expecting a class to need its own code before its objects can use the methods defined in Object.

    Why it fails

    Methods defined in Object are inherited by every other class, and the source says they can be used with any object.

    Do this instead

    Recognize that inherited methods are available automatically, with no extra code in your class.

  • Common mistake

    Stopping the chain at the first parent

    What it looks like

    Tracing Dog only as far as Animal and concluding that Animal is the top.

    Why it fails

    Every chain of superclasses ends at Object, even when it has several steps.

    Do this instead

    Keep following parents upward until you reach Object.

Checking Your Understanding of the Root

EASY

Consider these three declarations: class Bird { }, class Penguin extends Bird { }, and class Rock { }. Without running any code, state the direct superclass of each class. Then trace the full chain of superclasses for Penguin until you reach the top.

Hints
  • Bird and Rock have no extends clause, so ask what their direct superclass is.
  • Penguin's direct superclass is named in its extends clause. Then ask what that class's superclass is.

Key Takeaways

  • Object is a special class in java.lang and the one class in Java that is not a subclass of any other class.
  • Every other class is a subclass of some class, and a class with no explicit parent automatically becomes a subclass of Object.
  • Every chain of superclasses, however long, ends at Object, which sits at the root of the class structure.
  • Because every class is a subclass of Object, a variable of type Object can refer to an object of any class.
  • Methods defined in Object are inherited by every other class and can be used with any object without extra code in the class.