java
  1. java-object-class

Java Object Class

The Object class is the root class of all Java classes. Every class in Java is a direct or indirect subclass of the Object class. The Object class provides several methods that can be overridden by its subclasses.

Syntax

The Object class has the following syntax:

public class Object {
    protected Object clone() throws CloneNotSupportedException;
    public boolean equals(Object obj);
    protected void finalize() throws Throwable;
    public final Class<?> getClass();
    public int hashCode();
    public String toString();
}

Explanation

Here are the details of the Object class methods:

  • clone(): This method creates and returns a new object that is an exact copy of the current object.
  • equals(Object obj): This method checks if the current object is equal to the specified object. It returns true if the objects are equal, otherwise false.
  • finalize(): This method is called by the garbage collector when it determines that the object is no longer accessible.
  • getClass(): This method returns the Class object associated with the current object.
  • hashCode(): This method returns the hash code value for the current object.
  • toString(): This method returns a string representation of the current object.

Example

Here is an example of how the Object class is used in Java:

public class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == this) {
            return true;
        }

        if (!(obj instanceof Person)) {
            return false;
        }

        Person other = (Person) obj;

        return name.equals(other.name) && age == other.age;
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }

    @Override
    public String toString() {
        return "Person{name='" + name + "', age=" + age + "}";
    }
}

In the above example, we override the equals(), hashCode(), and toString() methods of the Object class to provide custom implementations for our Person class. This allows us to compare objects for equality based on their name and age, get a unique hash code for each object, and provide a string representation of the object.

Use

The Object class is used as a base class for all Java classes. It provides several methods that can be overridden by its subclasses. These methods allow for custom implementations of equals(), hashCode(), and toString(), and provide methods for cloning objects, getting the Class object associated with an object, and releasing resources when an object is no longer needed.

Important points

  • Every class in Java is a direct or indirect subclass of the Object class.
  • The Object class provides several methods that can be overridden by its subclasses.
  • Custom implementations of equals(), hashCode(), and toString() can be provided by a subclass of Object.
  • The Object class provides methods for cloning objects, getting the Class object associated with an object, and releasing resources when an object is no longer needed.

Summary

The Object class is the root class of all Java classes. It provides several methods that can be overridden by its subclasses to provide custom implementations of equals(), hashCode(), and toString(). The Object class also provides methods for cloning objects, getting the Class object associated with an object, and releasing resources when an object is no longer needed.

Published on: