Java Object Class

 

In Java, the Object class is the root class of all other classes. Every class in Java is either directly or indirectly derived from the Object class. The Object class provides a set of common methods and properties that are inherited by all other classes.

 


 

  Here are some of the methods provided by the Object class:

1.     equals(Object obj): This method is used to compare two objects for equality. It returns true if the objects are equal, otherwise false.

2.     hashCode(): This method returns the hash code value for the object. The hash code is used for hashing algorithms and data structures such as hash tables.

3.     toString(): This method returns a string representation of the object. By default, this method returns the class name and the memory address of the object in hexadecimal format.

4.     getClass(): This method returns the class object of the object.

5.     clone(): This method creates a new object that is a copy of the original object.

6.     finalize(): This method is called by the garbage collector before the object is destroyed. It can be overridden to perform finalization tasks such as releasing resources.

These are just a few of the methods provided by the Object class. Because all other classes in Java inherit from Object, these methods are available in all classes.

Here are some examples of how the Object class is used in Java:

  1. Overriding the equals method:

java

public class Person {

    private String name;

    private int age;

   

    // constructor, getters, and setters omitted for brevity

   

    @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;

    }

}

In this example, the equals method is overridden to compare Person objects based on their name and age fields, rather than just comparing object references.

  1. Using the hashCode method:

java

public class Person {

    private String name;

    private int age;

   

    // constructor, getters, and setters omitted for brevity

   

    @Override

    public int hashCode() {

        return Objects.hash(name, age);

    }

}

In this example, the hashCode method is overridden to generate a hash code based on the name and age fields of a Person object. This allows Person objects to be used as keys in hash tables and other data structures that rely on hash codes.

  1. Using the toString method:

java

public class Person {

    private String name;

    private int age;

   

    // constructor, getters, and setters omitted for brevity

   

    @Override

    public String toString() {

        return "Person{name='" + name + "', age=" + age + "}";

    }

}

In this example, the toString method is overridden to return a custom string representation of a Person object, which includes the name and age fields.

  1. Using the getClass method:

java

public class MyClass {

    // class implementation omitted for brevity

   

    public void printClassName() {

        System.out.println(getClass().getName());

    }

}

In this example, the getClass method is used to get the runtime class of a MyClass object, which is then used to print the class name to the console.

  1. Using the wait, notify, and notifyAll methods for inter-thread communication and synchronization:

java

public class SharedResource {

    private boolean flag = false;

   

    public synchronized void waitForFlag() throws InterruptedException {

        while (!flag) {

            wait();

        }

        System.out.println("Flag is now true!");

    }

   

    public synchronized void setFlag() {

        flag = true;

        notifyAll();

    }

}

In this example, the SharedResource class uses the wait, notify, and notifyAll methods to synchronize access to a shared flag variable. The waitForFlag method waits for the flag to become true, while the setFlag method sets the flag to true and notifies any waiting threads.