Java OOP – Chapter 8: The Object Class and Method Overriding in Java
T
Tuan Beo

Java OOP – Chapter 8: The Object Class and Method Overriding in Java

In this lesson, you'll learn how the Object class—the root of Java’s class hierarchy—provides core methods like toString(), equals(), and hashCode(). You’ll also understand how and why to override these methods to make your classes more useful and readable.

The Object Class

Every class in Java implicitly extends the Object class, even if you don’t write extends Object.

It provides several important methods, including:

toString()

Returns a string representation

equals(Object)

Compares objects for logical equality

hashCode()

Used for hashing-based collections

getClass()

Returns the runtime class of an object

clone()

Creates and returns a copy

finalize()

Called before garbage collection

Overriding toString()

By default, toString() returns something like:

Book@2f92e0f4

To get readable output, override it:

class Book {
    String title;
    String author;

    Book(String title, String author) {
        this.title = title;
        this.author = author;
    }

    @Override
    public String toString() {
        return title + " by " + author;
    }
}

Usage:

Book b = new Book("Clean Code", "Robert Martin");
System.out.println(b); // Clean Code by Robert Martin

Overriding equals() and hashCode()

By default, equals() compares object references (i.e., are they the same instance).

To compare logical equality (e.g., two books with the same title and author), override equals():

@Override
public boolean equals(Object obj) {
    if (this == obj) return true;
    if (obj == null || getClass() != obj.getClass()) return false;
    Book book = (Book) obj;
    return title.equals(book.title) && author.equals(book.author);
}

Whenever you override equals(), you should also override hashCode() to keep behavior consistent in hash-based collections (like HashMap or HashSet):

@Override
public int hashCode() {
    return Objects.hash(title, author);
}

Note: Objects.hash() is a utility method introduced in Java 7.

Summary

  • The Object class is the superclass of all Java classes.

  • Override toString() for readable output.

  • Override equals() and hashCode() for logical equality.

  • Always override both equals() and hashCode() together.

Assignment: Lesson 8 – Object Class and Method Overriding

Objective:

Understand and apply method overriding to customize class behavior.

Part 1: Conceptual Questions

Answer briefly:

  1. Why is toString() useful?

  2. What is the default behavior of equals()?

  3. Why should you override hashCode() when overriding equals()?

Part 2: Coding Task

Step 1:

Create a Movie class with fields:

  • title (String)

  • director (String)

  • year (int)

Step 2:

  • Override toString() to return: "title (year) directed by director"

  • Override equals() to consider two movies equal if title and year match.

  • Override hashCode() accordingly.

Step 3:

In main():

  • Create two movie objects with the same title and year.

  • Print them using System.out.println().

  • Compare them using equals().

  • Add them to a HashSet and show only one gets stored.

Github: https://github.com/tuanbeovnn/java-bootcamp/tree/java-bootcamp-oop-chapter8

Comments