Multiple choice technology

Given: 1. public class Person { 2. private String name; 3. public Person(String name) { this.name = name; } 4. public boolean equals(Person p) { 5. return p.name.equals(this.name); 6. } 7. } Which statement is true?

  1. The equals method does NOT properly override the Object.equals method.

  2. Compilation fails because the private attribute p.name cannot be accessed in line 5.

  3. To work correctly with hash-based data structures, this class must also implement the hashCode method.

  4. When adding Person objects to a java.util.Set collection, the equals method in line 4 will prevent duplicates.

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

The equals method shown does NOT properly override Object.equals(Object obj) because it takes a Person parameter instead of Object. This violates the overriding contract - the signature must match. For proper overriding, the parameter should be Object with an instanceof check inside. This equals method will not be called polymorphically when a Person is compared to a non-Person Object.