• ↑↓ pour naviguer
  • pour ouvrir
  • pour sélectionner
  • ⌘ ⌥ ↵ pour ouvrir dans un panneau
  • ←→ pour naviguer
  • esc pour rejeter
⌘ '
raccourcis clavier

The Problem

Every object in Java needs a basic set of operations — comparing with other objects, generating a hash code, producing a string representation, and being cloneable. Without a common base class, each class would redefine these fundamental behaviors inconsistently.

Core Idea

java.lang.Object is the root of the Java class hierarchy. Every class implicitly extends Object. It provides a set of core methods that all objects inherit: equals(), hashCode(), toString(), clone(), finalize(), getClass(), notify(), wait(), and notifyAll().

How It Works

When a class is defined without an explicit extends clause, the compiler adds extends Object. All inherited methods can be overridden. The default equals() uses reference equality (==); hashCode() returns the memory address (typically); toString() returns ClassName@hashCode.

Visual Explanation

java_object_class Object java.lang.Object (Root of Hierarchy) Str toString() Object->Str Eq equals() / hashCode() Object->Eq Clone clone() Object->Clone Thread wait() / notify() Object->Thread Final finalize() Object->Final Class getClass() Object->Class

Semantic Network

semantic_object_class THIS Object Class INHER Inheritance THIS--INHER built from STR Strings THIS--STR builds into COLL Collections THIS--COLL builds into THREAD Multithreading THIS--THREAD builds into

Key Properties

  • equals/hashCode contract: If two objects are equal, they must have the same hash code
  • Thread methods: wait() and notify() are defined here, making every object a monitor
  • getClass(): Returns the runtime class — final, cannot be overridden
  • finalize(): Called by GC before reclaiming memory (deprecated in Java 9+)

Connections

Edge Cases & Gotchas

  • equals() without hashCode(): Breaking the contract causes HashMap/HashSet to malfunction
  • clone() is tricky: It performs a shallow copy; overriding requires implementing Cloneable
  • finalize() is unreliable: Not guaranteed to run; use try-with-resources or Cleaner instead
  • toString() default: ClassName@1a2b3c4d is usually not human-readable