What is an Object class in Java?
Table of Contents
Short answer
Object
class is the super class of every class you can create.
In Java, a class extends another class using the keyword extends
. If you don't have any other class to extend, that's fine. The compiler will make your class extend the Object
class.
// your class
class Template {
// ...
// ...
}
// How compiler interprets your class
class Template extends Object {
// ...
// ...
}
Note: A class in Java can only extend one class in Java, multiple inheritance is not allowed.
So, every class in Java extends the Object
class (the default super class).
More detailed answer
In the world of Java programming, the Object
class holds a special place. It serves as the foundation for all other classes, acting as the ultimate ancestor of every Java object.
The root of all classes
In Java, every class is directly or indirectly derived from the Object
class. This means that every object in Java inherits certain fundamental behaviors from this class.
Key methods of Object class
The Object
class defines several methods that are inherited by all other classes. Some of the most commonly used methods include:
toString()
: This method returns a string representation of the object. It is often overridden in subclasses to provide a meaningful string representation of the object's state.equals(Object obj)
: This method is used to compare the current object with another object for equality.hashCode()
: This method returns a hash code value for the object. It is used in hash-based data structures likeHashMap
.getClass()
: This method gives you the class of the object.clone()
: This method creates and returns a copy of the object. Stay away from this method.finalize()
, you should not override it anymore, because it is deprecated and whatever you put in this method most of the time is buggy.
So, in a nutshell, only toString()
, equals()
, and hashCode()
, these are the most used ones.
Customization through Overriding
While the Object
class offers default implementations for these methods, they can be overridden in subclasses to tailor their behavior according to specific requirements.
Reference Type and Polymorphism
When an object is created in Java, it is essentially a reference to an instance of a class. This reference type is, by default, of type Object
. This characteristic allows for polymorphism, where a variable of type Object
can point to objects of different types.
Garbage Collection Considerations
The Object
class features a method called finalize()
, which can be overridden to perform cleanup actions before an object is automatically garbage collected.
Objects and Arrays
In Java, arrays are also treated as objects, and they inherit behaviors from the Object
class. This means that arrays can utilize methods defined in the Object
class.
Demo
Here's an example showing how the toString()
method can be overridden in a subclass:
class MyClass { // compiler see this as: class MyClass extends Object {
private int value;
public MyClass(int value) {
this.value = value;
}
@Override
public String toString() {
return "Value: " + value;
}
}
In this example, MyClass
overrides the toString()
method to provide a custom string representation of its instances.
Remember, even if you don't explicitly extend a class in Java, it implicitly extends Object
. So, every class in Java is a subclass of Object
.
Understanding the Object
class is pivotal in Java programming. It forms the bedrock upon which all other classes and objects are built. Its methods and behaviors serve as a powerful toolset for creating robust and flexible Java applications.
Gopi Gorantala Newsletter
Join the newsletter to receive the latest updates in your inbox.