Concepts / Programming with Objects

Programming with Objects

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

Every Class Has a Parent

Most of what you have studied about object-oriented programming so far has centered 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. This article looks at a side of the same system that 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, and it is the one class in Java that is not a subclass of any other class. Keep this single exception in mind, because it explains almost everything else in this article. As you read, ask yourself a simple question about each class you meet: what is its parent? By the end, you should be able to answer that question for any class, including classes that never mention a parent at all.

Tracing a Class Upward Through Its Parents

Start with any class and follow its parent, then that parent's parent, and so on. Each step moves one level up the class structure. The source describes this as a chain of parents. 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 stops at the same place.

The rule for a class with no explicit parent is the first thing to learn. A class that does not state an extends clause automatically becomes a subclass of Object. Trace a small example with three classes. Animal has no extends clause, so its parent is Object. Dog is written to extend Animal, so its parent is Animal. Car has no extends clause either, so its parent is Object. Follow any arrow upward and you reach Object.

Objectroot of every class chainAnimalno extends clauseDogextends AnimalCarno extends clausesuperclass ofsuperclass ofsuperclass of
Which class is each class's superclass, and where does every chain of parents end?

Read the diagram from the bottom up. Dog reaches Object through Animal. Because Animal is a subclass of Object, and Dog is a subclass of Animal, the rule that a subclass inherits from its parent applies at each step. The source does not walk through this exact example, but the result follows directly from its statements: every class except Object is a subclass of some other class, and a class with no explicit parent defaults to Object.

What do you think happens?

Car in the diagram has no extends clause. What is Car's superclass?

  • Animal

  • Dog

  • Object

  • Car has no superclass

Reveal answer
Answer:

Object

A class that states no extends clause automatically becomes a subclass of Object, so Object is Car's superclass even though nothing in Car's declaration names it.

Why Object Is the Root of the Structure

Definition

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.

Because every other class is a subclass of some class, and every class's chain of superclasses must end somewhere, the chain always terminates at Object. This is why the source describes Object as the root of the whole class structure. You can think of the class structure as a tree that grows downward from Object: every chain of parents, followed upward from any class you write, eventually reaches Object. Object is the one node in the tree that has no parent of its own.

Key point

Every class in Java is either Object itself or a subclass of some class that eventually leads back to Object. Object is the superclass of every other class, directly or through a longer chain.

Using an Object Variable for Any Class

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 idea is that the Object type sits above every class in the structure, so a reference typed as Object has a place for any of them. The short example below illustrates this relationship. It was written for this article to show the rule, and it is not taken from the source.

java
Notes on the code
  • Animal has no extends clause, so it is a subclass of Object.
  • Dog extends Animal, so it reaches Object through Animal.
  • Car has no extends clause, so it is a subclass of Object.
  • Each variable has the type Object but refers to an object of a different class.

Notice what the variables a, b, and c have in common. Their declared type is Object, yet each holds an object whose class you wrote yourself. The Object type is the common ground that all three classes share, which is exactly what the chain of parents predicts. Reading the code this way lets you see that the class structure, not any single class, is what makes this assignment possible.

Methods Every Object Can Use

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 inside a class for its objects to have access to the methods defined in Object.

Objectdefines instance methodsonceAnimalinherits themDoginherits them throughAnimalCarinherits themmethods inheritedmethods inheritedmethods inherited
How does a method defined only once in Object become usable on objects of classes you wrote yourself?

The diagram shows the key idea. The methods are written once, in Object, and they flow downward to every class beneath it. Animal and Car each inherit them directly. Dog inherits them through Animal, because Dog is a subclass of Animal and Animal is a subclass of Object. Your classes do not need to repeat these methods, and they do not need to declare any special support for them. Inheritance supplies the methods automatically, which is why they are available on objects of any class.

Mistakes Beginners Make About Object

  • Common mistake

    Believing a class with no extends clause has no parent

    What it looks like
    class Car { } is treated as if it stood alone in the program.
    Why it fails

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

    Do this instead

    Read a missing extends clause as meaning the superclass is Object.

  • Common mistake

    Thinking Object is just one ordinary parent among many

    What it looks like

    Assuming Object is one branch of the structure next to Animal and Car.

    Why it fails

    Object is the root. Every chain of parents ends at Object, so it sits above all the other branches.

    Do this instead

    Picture Object at the top of the structure and every other class below it.

  • Common mistake

    Writing the Object methods again in each class so they can be used

    Why it fails

    Every other class inherits the instance methods defined in Object, and those methods can be used with any object without special code.

    Do this instead

    Rely on inheritance. Write only the behavior that is specific to your class.

Checking Your Understanding of the Chain

EASY

A program declares three classes. Vehicle has no extends clause. Truck extends Vehicle. Bike has no extends clause. For each class, name its direct superclass, then trace the full chain of parents from Truck upward until you reach the top.

Hints
  • Start by checking which classes state an extends clause and which do not.
  • A class without an extends clause has Object as its direct superclass.
  • Truck's direct superclass is Vehicle, and Vehicle's direct superclass is Object.

Key Takeaways

  • Object is a special class in java.lang and is the one class in Java that is not a subclass of any other class.
  • A class that states no extends clause automatically becomes a subclass of Object, so Object is its superclass.
  • Every chain of parents, followed upward from any class, ends at Object, which makes Object the root of the class structure.
  • Because Object defines instance methods that every other class inherits, those methods can be used with any object without special code in each class.