← Back to Core Java Course | Chapter 7: OOP Core | Lesson 11 of 11

Java Object Class

Object is the ancestor of every Java class, so every object gets some basic abilities like describing itself and comparing itself. It is like a family trait everyone shares.
Syntax
java
@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?

java
class Dog {}
public class Main {
	public static void main(String[] args) {
		Dog dog = new Dog();
		System.out.println(dog.toString()); // inherited from Object
	}
}

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

java
// 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());
	}
}

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

java
// 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));
	}
}

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

java
// 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());
	}
}

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

java
// 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());
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Overriding equals() without hashCode(), which breaks HashSet and HashMap.
  2. Using == when equals() is needed.
  3. Not overriding toString(), so printing an object shows something like Person@1b6d3586.
Chapter Summary
  • Classes are blueprints and objects are instances, created and initialized with constructors, with this referring to the current object.
  • Access modifiers, non-access modifiers, and getters and setters control visibility and encapsulate data.
  • static, final, instanceof, and the Object class round out how objects behave.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.