Object Class in Java
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
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);
}
}java.lang.Object
true
false
String: Srihitha
Integer: 42
Double: 3.5How it works
Studenthas noextends, yet its superclass isjava.lang.Object.- An
Objectvariable can hold any object:Object any = s;. equalsis inherited fromObject. Its default compares references, so a secondStudentwith the same name isn't "equal". The next lessons fix that.describe(Object o)accepts aString, anInteger(42 is autoboxed) and aDouble: one method for every type.
Object's Methods
| Method | What it does | Override it? |
|---|---|---|
String toString() | A text form of the object; default is ClassName@hexHash | Often |
boolean equals(Object o) | Is this object "equal" to another? Default: same reference | When objects have a value identity |
int hashCode() | A number used by hash tables; must agree with equals | Whenever you override equals |
final Class<?> getClass() | The object's runtime class | Can't (it's final) |
protected Object clone() | A field-by-field copy, if the class is Cloneable | Rarely |
protected void finalize() | Called before garbage collection; deprecated for removal | Never |
final void wait(), wait(long), wait(long, int) | Thread waits for a notification | Can't |
final void notify(), notifyAll() | Wakes waiting threads | Can'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
- Easy: Print
getClass().getName()andgetClass().getSuperclass().getName()for aString, anIntegerand anint[]. - 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.
Key Takeaways
Was this tutorial helpful?