Object class methods and concepts

The Object class in Java is the root of the class hierarchy. Every class in Java inherits from Object, either directly or indirectly, making it the ultimate superclass. It provides a set of fundamental methods that all classes can use or override. These methods are defined in the java.lang.Object package and are available to every object in Java.

The Object Class

  • Provides default implementations for basic behaviors that all objects might need, such as equality comparison, string representation, and synchronization.
  • Key Characteristics:
    • All classes implicitly extend Object if they don’t explicitly extend another class.
    • It’s a concrete class, not abstract, so you can instantiate it (though this is rare).
    • It has no fields, only methods.

Methods of the Object Class

The Object class provides well defined methods, some of which are meant to be overridden by subclasses to provide more specific behavior. 

Java Program – Demonstrating Object Class Methods

class Product {
    private String name;
    private double price;

    public Product(String name, double price) {
        this.name = name;
        this.price = price;
    }

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

    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null || getClass() != obj.getClass()) return false;
        Product product = (Product) obj;
        return Double.compare(product.price, price) == 0 && name.equals(product.name);
    }

    @Override
    public int hashCode() {
        int result = name.hashCode();
        long priceBits = Double.doubleToLongBits(price);
        result = 31 * result + (int) (priceBits ^ (priceBits >>> 32));
        return result;
    }
}

public class ObjectClassDemo {
    public static void main(String[] args) {
        Product product1 = new Product("Laptop", 1500.0);
        Product product2 = new Product("Laptop", 1500.0);
        Product product3 = new Product("Phone", 800.0);

        // toString() demonstration
        System.out.println("Product 1: " + product1.toString());
        System.out.println("Product 2: " + product2);
        System.out.println("Product 3: " + product3);

        // equals() demonstration
        System.out.println("\nAre product1 and product2 equal? " + product1.equals(product2));
        System.out.println("Are product1 and product3 equal? " + product1.equals(product3));

        // hashCode() demonstration
        System.out.println("\nHashCode of product1: " + product1.hashCode());
        System.out.println("HashCode of product2: " + product2.hashCode());
        System.out.println("HashCode of product3: " + product3.hashCode());
    }
}
/*
Product 1: Product{name='Laptop', price=1500.0}
Product 2: Product{name='Laptop', price=1500.0}
Product 3: Product{name='Phone', price=800.0}

Are product1 and product2 equal? true
Are product1 and product3 equal? false

HashCode of product1: 103688
HashCode of product2: 103688
HashCode of product3: 54253
*/

The Object class is the root class of the Java class hierarchy. Every class in Java implicitly extends the Object class, either directly or indirectly. This class provides several essential methods that can be overridden to enhance the behavior of objects in custom classes.

Key Methods of the Object Class:

  • toString(): Returns a string representation of the object. Overriding this method provides a meaningful string output for custom objects.
  • equals(Object obj): Compares two objects for equality. It is necessary to override this method to ensure meaningful comparison based on object content.
  • hashCode(): Returns a unique integer representation of the object. It is often overridden alongside equals() to maintain the contract between the two.
  • getClass(): Returns the runtime class of the object.
  • clone(): Creates a shallow copy of the object. The class must implement the Cloneable interface for this method to work.
  • finalize(): Called by the garbage collector when the object is about to be destroyed.
  • notify(), notifyAll(), and wait(): Methods used for inter-thread communication.

By overriding the toString(), equals(), and hashCode() methods, developers can provide custom behavior and logical consistency when dealing with object comparison and representation. Thus, understanding the Object class is fundamental to building robust and reusable Java classes.

Scroll to Top