9.7 Object Superclass

  • The Object class is the superclass of all other classes as well as data types and is a part of the java.lang package

  • If a parent class doesn’t specify by using the extends keyword, the class will inherit from the Object

  • String toString() and boolean equals(object other) are the most frequently used and subclasses of Object override the equals and toString methods by using class-specific implementations

Normally when you use the toString method without overriding it, it returns a hash code value as seen in the example below:

class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
}

public class ToStringNoOverride {
    public static void main(String[] args) {
        Person person = new Person("Alice", 25);
        // When you print this out, it will return the hash code since its using the default method for toString
        System.out.println(person);
    }
}
ToStringNoOverride.main(null)
REPL.$JShell$15C$Person@4479c711

However, we can fix this by creating a new method by overriding the normal toString method and building our own representation of the object

class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() { // We override the toString method here
        return "Person Name: " + name + "\nPerson Age: " + age; // We then add our own custom string representation
    }
}

public class ToStringOverride {
    public static void main(String[] args) {
        Person person = new Person("Alice", 25);

        // When you print the person object, it calls the overridden toString method we made
        System.out.println(person);
    }
}
ToStringOverride.main(null)
Person Name: Alice
Person Age: 25

Now, we want to compare 2 people by using boolean equals(Object other) and determine if they are the same

class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person Name: " + name + "\nPerson Age: " + age;
    }
}

public class EqualsNoOverride {
    public static void main(String[] args) {
        Person person1 = new Person("Alice", 25);
        Person person2 = new Person("Alice", 25);

        System.out.println("Person 1 equals Person 2: " + person1.equals(person2)); // use the equals method to compare the two people
    }
}
EqualsNoOverride.main(null)
person1 equals person2: false

False is returned when you use the boolean equals(Object other) because the parameters is of Type Object superclass is unable to detect them since the Object class saves them under two different memory addresses and to fix this, we have to make an override method for this.

class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override // We override the equals method
    public boolean equals(Object other) {
        if (!(other instanceof Person)) // Using instanceof, we are making sure that the we compare objects of the same class
            return false;
        Person that = (Person) other; // Here we perform casting to make sure that other is converted to Person
        return this.age == that.age;
    }

    @Override
    public String toString() {
        return "Person Name: " + name + "\nPerson Age: " + age;
    }
}

public class EqualsOverride {
    public static void main(String[] args) {
        Person person1 = new Person("Alice", 25);
        Person person2 = new Person("Bryce", 25);

        System.out.println("Person 1 equals Person 2 (Age Only): " + person1.equals(person2));
    }
}
EqualsOverride.main(null)
Person 1 equals Person 2 (Age Only): true

Popcorn Hacks: Using a topic of your choice create your own override of one of the above