Object Class in Java

Naveen AnamaneniUpdated · 4 min read

Every class you write has a parent, even when you don't write extends. If no parent is named, the compiler makes the class extend java.lang.Object. So Object sits at the top of every class hierarchy, and every object in Java, whether it's a String, an array or your own Student, inherits its methods. Knowing those few methods well is the foundation for this whole module.

The Root of Every Class

Every class inherits these methods
Root.java▶ Try it
public class Root {
    static class Student {
        String name;
        Student(String name) { this.name = name; }
    }
 
    static void describe(Object o) {
        System.out.println(o.getClass().getSimpleName() + ": " + o);
    }
 
    public static void main(String[] args) {
        Student s = new Student("Navika");
        System.out.println(s.getClass().getSuperclass().getName());
 
        Object any = s;
        System.out.println(any.equals(s));
        System.out.println(any.equals(new Student("Navika")));
 
        describe("Srihitha");
        describe(42);
        describe(3.5);
    }
}
Output
java.lang.Object
true
false
String: Srihitha
Integer: 42
Double: 3.5

How it works

  1. Student has no extends, yet its superclass is java.lang.Object.
  2. An Object variable can hold any object: Object any = s;.
  3. equals is inherited from Object. Its default compares references, so a second Student with the same name isn't "equal". The next lessons fix that.
  4. describe(Object o) accepts a String, an Integer (42 is autoboxed) and a Double: one method for every type.

Object's Methods

MethodWhat it doesOverride it?
String toString()A text form of the object; default is ClassName@hexHashOften
boolean equals(Object o)Is this object "equal" to another? Default: same referenceWhen objects have a value identity
int hashCode()A number used by hash tables; must agree with equalsWhenever you override equals
final Class<?> getClass()The object's runtime classCan't (it's final)
protected Object clone()A field-by-field copy, if the class is CloneableRarely
protected void finalize()Called before garbage collection; deprecated for removalNever
final void wait(), wait(long), wait(long, int)Thread waits for a notificationCan't
final void notify(), notifyAll()Wakes waiting threadsCan't

That's 11 methods in all (counting the three wait overloads). wait and notify are covered with multithreading later in the course; the rest each get a lesson in this chapter.

Common Mistakes

Thinking equals compares contents by default. It compares references until you override it.

Overriding equals without hashCode. Hash-based collections then break (see "Object Equality and Hash Codes in Java").

Using Object parameters everywhere. Generics (covered later in the course) give type safety; Object loses it.

Interview Corner

What is the Object class in Java?

java.lang.Object is the root of the class hierarchy: every class extends it directly or indirectly, and arrays are objects too. It defines methods every object inherits: toString, equals, hashCode, getClass, clone, finalize (deprecated), and wait, notify and notifyAll for thread coordination.

Which Object methods are final?

getClass, notify, notifyAll and the three wait methods. They can't be overridden, because the JVM depends on their exact behavior.

Practice

  1. Easy: Print getClass().getName() and getClass().getSuperclass().getName() for a String, an Integer and an int[].
  2. Harder: Write static void printAll(Object... items) that prints each item's simple class name and value, and call it with five different kinds of values.

Quick check: Object class

4 questions
1What does a class extend if it has no extends clause?
2What does Object.equals compare by default?
3Which Object method can't be overridden?
4Which Object method is deprecated for removal?

Key Takeaways

Was this tutorial helpful?

Lesson 151 of 482