Every Java class implicitly inherits from java.lang.Object. Which methods does that give every object, and what is each for?
answer
- equals/hashCode/toString = the override-trio
- getClass and wait/notify/notifyAll are final
- clone is protected + needs Cloneable
- finalize is deprecated since Java 9
- Object is the universal root, default equals = ==
basics
~10 sEvery object inherits from Object: equals (logical equality), hashCode (an int for hash collections), toString (a text form), getClass (its runtime class), clone (copying), the deprecated finalize, plus wait/notify/notifyAll for thread coordination.
solid answer
~40 sIn Java every class extends java.lang.Object directly or indirectly, so every object inherits a fixed set of methods. The everyday ones are equals(Object) for logical equality, hashCode() returning an int used by hash-based collections, and toString() for a human-readable representation; the defaults are identity-based, so you usually override these three together (equals/hashCode in tandem). getClass() returns the runtime Class object and is final. clone() (protected) makes a field-by-field copy but needs the Cloneable marker interface. finalize() was a pre-GC hook, now deprecated. wait(), notify() and notifyAll() are final methods for the intrinsic-lock wait/notify coordination protocol and must be called while holding the object's monitor. Knowing which are final, protected, or deprecated explains what you can and cannot override.
go deeper
Can list the common inherited methods (equals, hashCode, toString) and knows every class extends Object.
Knows the full set, which are final/protected/deprecated, and that default equals/hashCode are identity-based.
Explains why each modifier exists (final protects runtime invariants, protected limits clone/finalize), and the override-together rule for equals/hashCode.
Reasons about API/design implications: why a universal root constrains the type system, finalize deprecation toward Cleaner, and the monitor methods living on Object rather than a separate type.
## Why every object has these methods In Java, **`java.lang.Object`** sits at the very top of the class hierarchy. If you write `class Foo {}` with no `extends`, the compiler implicitly makes it `class Foo extends Object`. So *every* object — including arrays — inherits Object's methods. This guarantees a common minimum API that the JDK and frameworks can rely on (collections, debuggers, `System.out.println`, etc.). ## The inherited methods, one by one - **`public boolean equals(Object obj)`** — *logical equality*. The default implementation compares **references** (`this == obj`): two variables are equal only if they point to the very same object. Override it to define equality by content (e.g. two `Point(1,2)` instances being equal). - **`public int hashCode()`** — returns an `int` used to bucket the object in **hash-based collections** (`HashMap`, `HashSet`). The default derives a value from the object's identity. Rule: if two objects are `equals`, they **must** have the same `hashCode`, so you override the two together. - **`public String toString()`** — a textual representation. Default is `getClass().getName() + "@" + Integer.toHexString(hashCode())`, e.g. `com.acme.Point@1b6d3586`. Override to produce something readable; it is auto-invoked in string concatenation and `println`. - **`public final Class<?> getClass()`** — returns the **runtime** `Class` object describing the actual type. It is **`final`**, so you cannot override it — the JVM must report the true type for reflection and safety. - **`protected Object clone()`** — produces a *field-by-field (shallow)* copy. It is **`protected`** and only works if the class implements the **`Cloneable`** marker interface; otherwise it throws `CloneNotSupportedException`. Effective Java discourages it in favor of copy constructors/factories. - **`protected void finalize()`** — historically called by the garbage collector before reclaiming an object. **Deprecated since Java 9** (unpredictable, slow, can resurrect objects); replaced by try-with-resources and `Cleaner`. - **`public final void wait()` / `wait(long)` / `wait(long,int)`, `public final void notify()`, `public final void notifyAll()`** — the intrinsic-lock **wait/notify** thread-coordination protocol. They are **`final`** (the protocol is fixed) and must be called while holding the object's monitor (else `IllegalMonitorStateException`). ## Which are final / protected / deprecated, and why it matters | Method | Modifier | Can you override? | Note | |---|---|---|---| | `equals`, `hashCode`, `toString` | public | Yes | The three you normally override | | `getClass` | public **final** | No | Must report true runtime type | | `clone` | **protected** | Yes (often re-declared public) | Needs `Cloneable` | | `finalize` | protected, **deprecated** | Yes but don't | Use `Cleaner` instead | | `wait`/`notify`/`notifyAll` | public **final** | No | Monitor protocol is fixed | The modifiers encode design intent: `final` blocks overriding things the runtime depends on; `protected` limits casual external calls (clone, finalize); `deprecated` signals 'avoid'.
- Why are wait/notify/notifyAll declared final on Object?They implement the JVM's fixed monitor wait-set protocol tied to every object's intrinsic lock; allowing overrides could corrupt that contract, so the language forbids it.
- Which Object methods are most commonly overridden in practice?equals, hashCode and toString. equals and hashCode are overridden together to keep their contract; toString is overridden for readable logging/debugging.
saying these in an interview costs you the question
- Claiming you can override getClass() or wait()/notify() (they are final)
- Saying clone() works on any class without implementing Cloneable
- Thinking finalize() reliably frees resources at a known time
- Overriding equals() but not hashCode() (breaks hash collections)
- Assuming default equals() compares contents rather than references