In Java, the Object
class is a special class that is the root of the class hierarchy. Every class in Java is directly or indirectly derived from the Object
class. This means that the Object
class is the superclass for all Java classes, and it provides some fundamental methods that are available to all objects in Java.
Here are some key points about the Object
class:
- Methods in the
Object
Class:
- The
Object
class provides several methods that can be overridden by any class. Some of the commonly used methods include:toString()
: Returns a string representation of the object.equals(Object obj)
: Indicates whether some other object is “equal to” this one.hashCode()
: Returns a hash code value for the object.getClass()
: Returns the runtime class of an object.clone()
: Creates and returns a copy of the object.
- Default Implementation:
- The methods in the
Object
class have default implementations. However, these implementations may not be suitable for all classes. - It’s common for classes to override the
toString()
andequals(Object obj)
methods to provide meaningful representations and custom equality checks for objects.
- Implicit Inheritance:
- Since every class in Java inherits from
Object
, if a class does not explicitly extend another class, it implicitly extendsObject
. - For example:
public class MyClass { // ... }
This is equivalent to:public class MyClass extends Object { // ... }
- Usage of
Object
in Collections:
- Collections in Java, such as
ArrayList
orHashMap
, can hold objects of any type. Since all classes extendObject
, collections can hold objects of diverse types.
List<Object> myList = new ArrayList<>();
myList.add("String");
myList.add(42);
In the example above, both a String
and an Integer
can be added to the list because both types ultimately extend Object
.
The Object
class is fundamental to the Java language and provides a common interface for all classes. Understanding and utilizing the methods from the Object
class is essential when designing and working with Java classes.
Object, defined by Java. All other classes are subclasses of Object. That is, Object is a superclass of all other classes. This means that a reference variable of type Object can refer to an object of any other class
Object defines the following methods, which means that they are available in every object
Table of object class
Method | Purpose |
---|---|
Object clone( ) | Creates a new object that is the same as the object being cloned |
boolean equals(Object object) | Determines whether one object is equal to another. |
void finalize( ) | Called before an unused object is recycled. |
Class getClass( ) | Obtains the class of an object at run time. |
int hashCode( ) | Returns the hash code associated with the invoking object |
void notify( ) | Resumes execution of a thread waiting on the invoking object. |
void notifyAll( ) | Resumes execution of all threads waiting on the invoking object |
String toString( ) | Returns a string that describes the object |
void wait( ) void wait(long milliseconds) void wait(long milliseconds, int nanoseconds) | Waits on another thread of execution. |