Object Class
Root class of all Java classes
5 min read
Object Class in Java
The Object class is the root of the Java class hierarchy. Every class in Java implicitly extends Object, inheriting its methods.
Important Object Methods
- toString(): String representation of object
- equals(): Check equality with another object
- hashCode(): Hash code for hashing data structures
- getClass(): Runtime class information
- clone(): Create a copy (if Cloneable)
- finalize(): Called before garbage collection (deprecated)
- wait(), notify(), notifyAll(): Thread synchronization
🔑 Key Point: Even if you don't write extends Object, your class automatically extends it. That's why you can call toString() on any object!
Code Examples
Object as the root class
java
1// Every class extends Object
2public class MyClass {
3 // Implicitly: extends Object
4}
5
6// All these are valid
7Object obj1 = new MyClass();
8Object obj2 = new String("Hello");
9Object obj3 = new Integer(42);
10Object obj4 = new int[]{1, 2, 3};
11
12// Object methods available on any object
13MyClass mc = new MyClass();
14mc.toString(); // Inherited from Object
15mc.equals(obj1); // Inherited from Object
16mc.hashCode(); // Inherited from Object
17mc.getClass(); // Inherited from Object
18
19// getClass() returns runtime type
20Object mystery = "Hello";
21System.out.println(mystery.getClass().getName()); // java.lang.String
22System.out.println(mystery.getClass().getSimpleName()); // StringDefault behavior of Object methods
java
1// Default Object methods behavior
2public class Person {
3 private String name;
4 private int age;
5
6 public Person(String name, int age) {
7 this.name = name;
8 this.age = age;
9 }
10}
11
12Person p1 = new Person("Alice", 25);
13Person p2 = new Person("Alice", 25);
14
15// Default toString() - class name + @ + hash code
16System.out.println(p1.toString()); // Person@15db9742
17
18// Default equals() - reference comparison (==)
19System.out.println(p1.equals(p2)); // false! Different objects
20
21// Default hashCode() - typically based on memory address
22System.out.println(p1.hashCode()); // Some integer
23System.out.println(p2.hashCode()); // Different integer
24
25// That's why we override these methods!Properly overriding Object methods
java
1// Properly overriding Object methods
2public class Person {
3 private String name;
4 private int age;
5
6 public Person(String name, int age) {
7 this.name = name;
8 this.age = age;
9 }
10
11 @Override
12 public String toString() {
13 return "Person{name='" + name + "', age=" + age + "}";
14 }
15
16 @Override
17 public boolean equals(Object obj) {
18 if (this == obj) return true;
19 if (obj == null || getClass() != obj.getClass()) return false;
20 Person person = (Person) obj;
21 return age == person.age && Objects.equals(name, person.name);
22 }
23
24 @Override
25 public int hashCode() {
26 return Objects.hash(name, age);
27 }
28}
29
30Person p1 = new Person("Alice", 25);
31Person p2 = new Person("Alice", 25);
32
33System.out.println(p1.toString()); // Person{name='Alice', age=25}
34System.out.println(p1.equals(p2)); // true! Same values
35System.out.println(p1.hashCode() == p2.hashCode()); // true!Other useful Object methods
java
1// Other Object methods
2public class Demo {
3 public static void main(String[] args) {
4 Object obj = new String("Hello");
5
6 // getClass() - runtime type info
7 Class<?> clazz = obj.getClass();
8 System.out.println(clazz.getName()); // java.lang.String
9 System.out.println(clazz.isArray()); // false
10 System.out.println(clazz.getSuperclass()); // class java.lang.Object
11
12 // instanceof vs getClass()
13 Object str = "Hello";
14 System.out.println(str instanceof String); // true
15 System.out.println(str instanceof Object); // true
16 System.out.println(str.getClass() == String.class); // true
17
18 // clone() - needs Cloneable interface
19 // finalize() - deprecated, don't use
20
21 // Thread methods (wait, notify, notifyAll)
22 // Used for inter-thread communication
23 // Requires synchronized block
24 }
25}Use Cases
- Base type for generic code
- toString() for debugging/logging
- equals()/hashCode() for collections
- getClass() for reflection
- Polymorphic containers (List<Object>)
- Thread synchronization primitives
Common Mistakes to Avoid
- Not overriding equals/hashCode together
- Using finalize() for cleanup
- Forgetting getClass() returns runtime type
- Using == instead of equals()
- Not understanding Object is always parent
- Incorrect clone() implementation