Object
About
The Object
class is the root of the class hierarchy in Java. Every class in Java directly or indirectly inherits from the Object
class. It provides basic methods that all Java objects share, such as equality, hashing, cloning, and string representation. It resides in the java.lang
package, and no import is required to use it.
Features
Root Class: The base class for all Java classes.
Essential Methods: Provides methods for object comparison, hashing, string conversion, thread synchronization, and more.
Automatic Inheritance: All classes inherit from
Object
either directly or through other classes.Used in Collections: The
Object
class serves as a common data type in generic structures likeList<Object>
.
Internal Working
1. Inheritance and Subtyping
Any class can be cast to
Object
.This allows collections and frameworks to handle objects generically.
2. Methods Overview
The Object
class defines several methods that are fundamental to Java programming:
equals
: Determines logical equality.hashCode
: Returns a hash code for the object.toString
: Provides a string representation of the object.clone
: Creates a shallow copy of the object.finalize
: Performs cleanup before garbage collection.wait
,notify
,notifyAll
: Used for thread synchronization.
Key Methods
Key Method
Description
equals(Object obj)
Compares the current object with the specified object for equality.
Default Implementation: Compares references (==
).
hashCode()
Returns an integer hash code representing the object.
Contract with equals
: If two objects are equal, they must have the same hash code.
toString()
Returns a string representation of the object.
Default format: ClassName@HashCode
.
clone()
Creates a shallow copy of the object.
Requirement: Must implement Cloneable
interface; otherwise, throws CloneNotSupportedException
.
wait(), notify(), notifyAll()
Thread synchronization methods for inter-thread communication. Usage: Used within synchronized blocks or methods.
finalize()
Called by the garbage collector before reclaiming memory.
Note: Deprecated due to unreliability and better alternatives like try-with-resources
.
Limitations
Default Behavior: The default implementations of methods like
equals
andhashCode
may not be suitable for all use cases and often need overriding.Synchronization Overhead: Methods like
wait
andnotify
require careful handling to avoid deadlocks.Deprecated Finalize:
finalize
is unreliable and not recommended for resource cleanup.
Real-World Usage
Equality and Hashing: Custom classes that use collections like
HashSet
orHashMap
often overrideequals
andhashCode
.Debugging:
toString
is commonly overridden to provide meaningful information about objects during debugging.Concurrency: Thread-safe communication using
wait
,notify
, andnotifyAll
.Generic Object Handling: Used in APIs and frameworks that work with objects generically (e.g.,
Object[]
, Reflection).
Examples
1. Default Methods
2. Overriding equals
and hashCode
equals
and hashCode
3. Using clone
clone
4. Synchronization with wait
and notify
wait
and notify
Last updated
Was this helpful?