AP® Computer Science A · Unit 1 · Topic 1.12

Class Hierarchies, Subclasses, and the Object Class

A class hierarchy lets a subclass reuse a superclass's non-private members without copying any code. Every class in Java ultimately extends Object, which is why method lookup for things like toString can always find something to run.

Put shared attributes and behavior in one parent class so every child class gets them for free. If Dog and Cat both need a name and an eat method, you write those once in Animal and let Dog and Cat extend Animal. This is inheritance: a subclass doesn't hold a pasted-in copy of the superclass's code, it holds a relationship. When you call a method on a Dog object, the JVM looks for that method starting at Dog, and if it isn't defined there, it walks up to Animal, then to Animal's superclass, and so on, running the first version it finds.

That walk always ends somewhere, because every class you write, directly or indirectly, extends Object. If a class has no explicit extends clause, Java silently makes it extend Object anyway. Object sits at the top of every hierarchy and defines a handful of methods every object inherits automatically, including toString and equals. So a chain like Circle extends Shape extends Object means a toString call on a Circle checks Circle first, then Shape, then Object, stopping at whichever level actually defines it.

This is exactly why any object can be dropped into a String with + concatenation: Java calls toString on it to get a String representation, and because toString traces back to Object, that call never fails to find a method, even if nobody along the chain bothered to override it. Understanding this lookup path matters once you start overriding methods yourself and calling super, since that keyword explicitly tells Java to look one level up rather than searching from the bottom.

Key terms

superclass
The class being extended, whose non-private members are available to its subclasses.
subclass
A class that extends another class and inherits access to its non-private members.
Object class
The topmost class in Java, which every other class extends directly or indirectly.
method lookup
The process of searching a class and then its ancestors, in order, for the first matching method definition.
override
Defining a method in a subclass with the same signature as one in a superclass, replacing it for that subclass.

Watch it explained

  1. How it worksHow does a subclass draw on a superclass without copying any of its code?
  2. How it worksWalk me through the hierarchy tree that puts every Java class underneath Object.
  3. Worked exampleTrace how a call to toString on a Circle finds its method by walking up the class tree.
  4. Common mistakeWhy isn't a subclass just a copy of its superclass with extra bits pasted on?
  5. Ties togetherHow does the Object class explain why any object at all can be concatenated onto a String?

Check yourself

Your turn1 of 2
A student says a Dog subclass of Animal 'copies' Animal's code and then adds its own extra methods. What's actually true?

End of topic.