Skip to content

What is an Object class in Java?

Gopi Gorantala
Gopi Gorantala
2 min read

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 like HashMap.
  • 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.

Java Interview HandbookJava

Gopi Gorantala Twitter

Gopi is an engineering leader with 12+ of experience in full-stack development—a specialist in Java technology stack. He worked for multiple startups, the European govt, and FAANG in India and Europe.

Comments


Related Posts

Members Public

Differences Between JDK, JRE, and JVM?

Short answer JDK, JRE, and JVM are essential components of the Java platform, each serving a distinct purpose. Here are the key differences between them: 1. JDK (Java Development Kit): The JDK is used by developers to write, compile, and debug Java code. 2. JRE (Java Runtime Environment): End-users use

Members Public

Difference Between String and char[] in Java

Short answer Strings String is an object with many helpful methods. String class in Java's standard library is designed to handle text as a sequence of characters. A string of characters (string object) is non-modifiable or immutable in Java. Once you've created it, you cannot modify

Members Public

What is the JVM? With Sketches and Examples

Short answer JVM stands for Java Virtual Machine. JVM is a virtual machine( a piece of software) that executes Java bytecode. It provides a runtime environment for executing Java applications. The above sketch explains how the same bytecode is run across multiple virtual machines on different operating systems. If someone