Object is the root superclas of all classes in Java.
Every class in Java implicitly inherits from java.lang.Object
.
Why is it important?
- Defines common behaviors for all Java objects.
- Enables Javaβs polymorphism, collections and runtime type handling.
Example
Object obj = new String("Hello");
System.out.println(obj.toString()); // "Hello"
System.out.println(obj.getClass()); // java.lang.String
Key Points
- You donβt need to extend Object - it happens automatically.
- Override equals(), hashCode() and toString() in custom classes.
- Object allows you to store any type (used in raw collections, generics).
Methods
equals() // Compares object content (override to customize)
hashCode() // Returns hash value (used in hash-based collections)
toString() // Returns a string representation of the object
getClass() // Returns runtime class info
clone() // Creates and returns a copy (requires Cloneable)
finalize() // Called by GC before object is destroyed (deprecated)
wait(), notify(), notifyAll() // Thread synchronization methods
Real-World Analogy
Like the base template from which every other object is made. Every tool, shape or widget starts from this base.
Tip
Be ready to answer:
- Why does every class extend
Object
?- Whatβs the use of
equals()
/hashCode()
/toString()
?- What happens if you donβt override them?
Parent: _OOP