Java Object Class
In this page:
@Override
public String toString() { ... }
@Override
public boolean equals(Object obj) { ... }
@Override
public int hashCode() { ... }
What is the Object Class?
Every class in Java implicitly extends Object if it doesn't explicitly extend anything else, which is why every object automatically has methods like toString(), equals(), and hashCode() available even if you never wrote them yourself.
Example: What is the Object Class?
class Dog {}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
System.out.println(dog.toString()); // inherited from Object
}
}
Login to try C/C++/Java/PHP code in the editor
The toString() Method
The default toString() inherited from Object prints the class name plus a hash code, which is rarely useful for debugging — overriding it to return meaningful field values is one of the most common customizations you'll make on your own classes.
Example: The toString() Method
// Define the class Dog
class Dog {
// Declare name and set it to "Rex"
String name = "Rex";
// Override the method inherited from the parent type
@Override
public String toString() {
// Send "Dog: " + name back to the caller
return "Dog: " + name;
}
}
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Print a line to the console
System.out.println(new Dog());
}
}
Login to try C/C++/Java/PHP code in the editor
The equals() Method
The default equals() from Object just compares memory references (identical to ==), so two logically-equal objects with different addresses would be considered unequal unless you override equals() to compare actual field values instead.
Example: The equals() Method
// Define the class Point
class Point {
int x;
Point(int x) { this.x = x; }
// Override the method inherited from the parent type
@Override
public boolean equals(Object obj) {
if (!(obj instanceof Point)) return false;
// Send ((Point) obj).x == this.x back to the caller
return ((Point) obj).x == this.x;
}
}
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Create a new Point object and store it in p1
Point p1 = new Point(5);
// Create a new Point object and store it in p2
Point p2 = new Point(5);
// Print a line to the console
System.out.println(p1.equals(p2));
}
}
Login to try C/C++/Java/PHP code in the editor
The hashCode() Method
hashCode() returns an integer used by hash-based collections like HashMap and HashSet to bucket objects efficiently; Java's contract requires that two objects considered equal by equals() must return the same hash code, or those collections will behave incorrectly.
Example: The hashCode() Method
// Define the class Point
class Point {
int x;
Point(int x) { this.x = x; }
// Override the method inherited from the parent type
@Override
public int hashCode() {
// Send Integer.hashCode(x) back to the caller
return Integer.hashCode(x);
}
}
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Print a line to the console
System.out.println(new Point(5).hashCode());
}
}
Login to try C/C++/Java/PHP code in the editor
The getClass() Method
getClass() returns the runtime Class object describing an instance's actual type, which is useful for reflection, logging the concrete class name, or distinguishing between subclasses when instanceof checks alone aren't precise enough.
Example: The getClass() Method
// Define the class Dog
class Dog {}
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Create a new Dog object and store it in dog
Dog dog = new Dog();
// Print a line to the console
System.out.println(dog.getClass().getName());
}
}
Login to try C/C++/Java/PHP code in the editor
- Overriding
equals()withouthashCode(), which breaksHashSetandHashMap. - Using
==whenequals()is needed. - Not overriding
toString(), so printing an object shows something likePerson@1b6d3586.
- Classes are blueprints and objects are instances, created and initialized with constructors, with
thisreferring to the current object. - Access modifiers, non-access modifiers, and getters and setters control visibility and encapsulate data.
static,final,instanceof, and theObjectclass round out how objects behave.
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: