OOP · Core Java OOP Features
Object Class
Imagine you're developing a Student Management System.
Real-World Problem
Imagine you're developing a Student Management System. You need to compare student objects, print their details, store them in collections like HashMap, or create duplicate objects. Writing separate methods for each task in every class would increase code duplication and complexity.
Java solves this by providing the Object class, which contains common methods that every Java object inherits. Why Do We Need the Object Class?
Almost every Java object needs common operations such as comparing objects, converting them into strings, generating hash codes, or creating copies. Instead of implementing these features repeatedly, Java provides them through the Object class, making development simpler and more consistent.

What is the Object Class? The Object class is the root class of the Java class hierarchy. Every class in Java directly or indirectly inherits from the Object class, allowing all objects to access its common methods.
Common Methods of the Object Class
1. equals()
Why Do We Need It? To compare whether two objects are logically equal instead of comparing only their memory addresses. What is equals()? The equals() method compares the contents of two objects and returns true if they are logically equal; otherwise, it returns false.
2. hashCode()
Why Do We Need It? Collections such as HashMap, HashSet, and Hashtable use hash codes to store and retrieve objects efficiently. What is hashCode()? The hashCode() method returns an integer value that represents the hash code of an object.
3. toString()
Why Do We Need It? Printing an object directly usually displays its memory reference. The toString() method provides a meaningful string representation of the object. What is toString()? The toString() method returns a string that describes the object's state in a human-readable format.
4. clone()
Why Do We Need It? Sometimes we need another object with the same data as an existing object without manually copying each field. What is clone()? The clone() method creates and returns a copy of an existing object.

Every class silently extends Object; overriding its methods gives objects sane defaults.
public class Money {
private final int cents;
Money(int cents) { this.cents = cents; }
@Override
public boolean equals(Object o) { // value equality, not reference
if (!(o instanceof Money)) return false;
return this.cents == ((Money) o).cents;
}
@Override
public int hashCode() { return Integer.hashCode(cents); }
@Override
public String toString() { return "$" + cents / 100.0; }
}