The Object Class
The java.lang.Object class is the parent class of all classes in Java by default. In other words, it is the topmost class of Java's class hierarchy.
If a class does not explicitly extend any other class, it implicitly extends Object. Therefore, the methods of the Object class are available to all Java classes.
Important Methods of the Object Class
The Object class provides several crucial methods that are frequently overridden by subclasses to provide specific behaviors.
1. toString() Method
The toString() method returns a string representation of the object.
- By default, it returns a string consisting of the class name, followed by the
@symbol, and the unsigned hexadecimal representation of the hash code of the object. - It is highly recommended to override this method in your classes to return a meaningful string representation of the object's state (its data).
class Student {
int rollno;
String name;
Student(int rollno, String name) {
this.rollno = rollno;
this.name = name;
}
// Overriding toString() to provide a meaningful representation
@Override
public String toString() {
return "Student [Roll No=" + rollno + ", Name=" + name + "]";
}
}
public class LabObject1 {
public static void main(String args[]) {
Student s1 = new Student(101, "Alice");
// When you print an object, the compiler implicitly calls toString()
System.out.println(s1);
}
}
Output:
Student [Roll No=101, Name=Alice]
2. equals(Object obj) Method
The equals() method is used to compare the equality of two objects.
- The default implementation in the
Objectclass simply checks if two references point to the exact same memory location (i.e.,obj1 == obj2). - We usually override this method to compare the actual contents (state) of the objects rather than their memory references.
class Employee {
int id;
Employee(int id) {
this.id = id;
}
// Overriding equals() to compare contents (ID)
@Override
public boolean equals(Object obj) {
if (this == obj) return true; // Same memory reference
if (obj == null || getClass() != obj.getClass()) return false;
Employee emp = (Employee) obj;
return this.id == emp.id; // Content comparison
}
}
public class LabObject2 {
public static void main(String args[]) {
Employee e1 = new Employee(500);
Employee e2 = new Employee(500);
System.out.println(e1 == e2); // false (different memory locations)
System.out.println(e1.equals(e2)); // true (same contents because we overrode equals)
}
}
3. hashCode() Method
The hashCode() method returns an integer hash code value for the object. This is primarily used in hashing-based collections like HashMap, HashSet, and Hashtable.
[!WARNING] The General Contract: If two objects are equal according to the
equals(Object)method, then calling thehashCode()method on each of the two objects must produce the same integer result. Therefore, if you overrideequals(), you must also overridehashCode().
4. clone() Method
The clone() method returns a shallow copy of the object.
- To use this method, the class must implement the
java.lang.Cloneablemarker interface; otherwise, aCloneNotSupportedExceptionis thrown.
5. finalize() Method
The finalize() method is called by the Garbage Collector on an object when garbage collection determines that there are no more references to the object.
- It is used to perform clean-up operations (like closing a database connection) just before the object is destroyed.
- Note: As of Java 9, the
finalize()method has been deprecated because it is unpredictable and often causes performance issues.
6. getClass() Method
Returns the runtime class of the object. It is a final method and cannot be overridden.
public class LabObject3 {
public static void main(String args[]) {
String s = new String("Hello");
System.out.println("The class is: " + s.getClass().getName());
}
}
Output:
The class is: java.lang.String