The Object Class
Every class in Java implicitly extends Object if it doesn't explicitly extend anything else. As covered on the Inheritance page, extends normally lets you choose a parent — but if you leave it out entirely, Java silently inserts extends Object for you. This means every single object you ever create — every String, every ArrayList, every custom class — inherits a baseline set of methods from Object.
Key inherited methods
Method | Purpose |
|---|---|
| Returns a String representation of the object, used by |
| Determines whether this object is considered equal to another |
| Returns an integer used to place the object in hash-based collections like |
| Returns the runtime |
Overriding toString()
Object's default toString() produces the class name followed by @ and a hexadecimal hash code — technically correct, but not remotely useful for debugging.
The default toString()
class Point {
int x, y;
Point(int x, int y) { this.x = x; this.y = y; }
}
Point p = new Point(3, 4);
System.out.println(p); // Point@1b6d3586 <- not usefulOverriding toString() to return something meaningful is common practice for almost every class you write, so that logging, printing, and debugging give you real information instead of a hash code.
A useful toString()
class Point {
int x, y;
Point(int x, int y) { this.x = x; this.y = y; }
@Override
public String toString() {
return "Point(" + x + ", " + y + ")";
}
}
Point p = new Point(3, 4);
System.out.println(p); // Point(3, 4)Point(3, 4)
The equals() / hashCode() contract
Object's default equals() compares references — two objects are “equal” only if they are literally the same object in memory. It's very common to override equals() so that two distinct objects are considered equal when their field values match, exactly the way records do automatically.
Overriding both together
import java.util.Objects;
class Point {
int x, y;
Point(int x, int y) { this.x = x; this.y = y; }
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Point)) return false;
Point other = (Point) o;
return x == other.x && y == other.y;
}
@Override
public int hashCode() {
return Objects.hash(x, y); // consistent with equals() above
}
}Every class implicitly extends
Objectunless it extends something else explicitly.toString(),equals(),hashCode(), andgetClass()are inherited by every object.Override
toString()for meaningful debug output — the default is just a class name and hash code.Always override
equals()andhashCode()together — never just one.