#An example that requires overriding equals method

1 messages · Page 1 of 1 (latest)

karmic ledge
#

Im just wondering if there's an example to look at, i been reading about "how" it should be done but hard to come up with an example. Or is it just whatever i need it to be based on design or my logic?

safe adderBOT
#

<@&987246399047479336> please have a look, thanks.

safe adderBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

#

Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#

Sure, here's an example that demonstrates the need for overriding the equals method:

public class Person {
    private String name;
    private int age;

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

    // Getters and setters

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null || getClass() != obj.getClass()) {
            return false;
        }
        Person person = (Person) obj;
        return age == person.age && Objects.equals(name, person.name);
    }

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

        System.out.println(person1.equals(person2)); // Output: false

        String john = "John";
        System.out.println(person1.equals(john)); // Output: false

        Integer age = 25;
        System.out.println(person1.equals(age)); // Output: false
    }
}

In this example, we have a Person class with two attributes - name and age. We override the equals method to compare two Person objects based on their name and age.

In the main method, we create two different instances of Person, but with the same name and age. However, when we compare them using the equals method, it returns false because by default, the equality comparison is based on object reference (i.e., whether they refer to the same memory location).

We also demonstrate that comparing a Person object with a different type (String and Integer) using the default implementation of equals also returns false.

By overriding the equals method, we can define our own logic for comparing objects and determine when two objects should be considered equal.

karmic ledge
#

oki this makes sense, if i want to make two entities equal in my universe based on my logic.

ebon moat
#

Yas

#

Good example