library
beginnerObject Class Methods
Know every method in java.lang.Object and when to override each one.
Every class extends Object, so every object has these methods whether you wrote them or not.
Object methods are like a government-issued ID card: everyone gets one by birth (inheritance). You can customize what's printed on it (override toString), but the card format (method signatures) is fixed.
Key Concepts
1
toString() returns ClassName@1b6d3586 by default, which tells you nothing:
toString()ClassName@1b6d3586
2
@Override
public String toString() { return "Point[x=" + x + ", y=" + y + "]"; }3
Worth overriding on anything you will ever log or debug, because println, string concatenation and your
debugger all call it.
println
4
equals(Object) compares references by default, so two objects with identical contents are unequal:
equals(Object)
5
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Point p)) return false; // handles null too
return x == p.x && y == p.y;
}6
It must be reflexive, symmetric, transitive, consistent, and false for null.
null
7
hashCode() must agree with it:
hashCode()
8
@Override
public int hashCode() { return Objects.hash(x, y); }9
Equal objects must have equal hash codes. Override equals without hashCode and HashMap stops
finding your keys — you put an entry in and get null back, because the lookup checks the wrong bucket.
equalshashCodeHashMapnull
10
clone() needs the Cloneable marker interface and gives a shallow copy, so mutable fields are shared
between the original and the copy. A copy constructor is clearer:
clone()Cloneable
11
Point(Point other) { this(other.x, other.y); }12
finalize() is deprecated since Java 9. Its timing is unpredictable and it can resurrect an object.
Use try-with-resources, or Cleaner if you truly need post-mortem cleanup.
finalize()Cleaner
13
getClass() is final and returns the runtime class, which is the entry point to reflection.
getClass()
14
wait(), notify(), notifyAll() coordinate threads on the object's monitor and throw
IllegalMonitorStateException outside a synchronized block.
wait()notify()notifyAll()IllegalMonitorStateExceptionsynchronized
15
What the interviewer is probing.1. "You override equals but not hashCode. What breaks?" Probing: the contract and its
consequence. Stalls: "Nothing much." Moves up: HashMap and HashSet stop finding the object: you
put it in and get null back, because the lookup computes a different bucket.
16
2. "What are the rules equals must satisfy?" Probing: whether you know the contract. Stalls:
"It compares the fields." Moves up: reflexive, symmetric, transitive, consistent, and false for
null — and that subclassing is where symmetry usually breaks.
17
3. "Why is clone() discouraged?" Probing: whether you can justify the common advice. Stalls:
"It is old." Moves up: it needs the Cloneable marker, returns a shallow copy that shares mutable
fields, and bypasses constructors; a copy constructor is clearer.
18
4. "What replaced finalize(), and why was it removed?" Probing: resource cleanup. Stalls:
"Garbage collection handles it." Moves up: its timing is unpredictable and it can resurrect
objects; try-with-resources handles the normal case and Cleaner the rest.