Skip to content
CodeKruPro

equals(Point) overloads instead of overriding

Predict the outputHardJava only
Main.java
import java.util.*;

class Point {
    int x;

    Point(int x) {
        this.x = x;
    }

    public boolean equals(Point other) {
        return other != null && x == other.x;
    }
}

public class Main {
    public static void main(String[] args) {
        Point p = new Point(1);
        Object o = new Point(1);
        System.out.println(p.equals(new Point(1)));
        System.out.println(p.equals(o));
        List<Point> list = new ArrayList<>();
        list.add(new Point(1));
        System.out.println(list.contains(p));
    }
}

This question is part of Pro+

Pro+ opens every question in Interview Prep and the mock interview. Every article and the first 2 questions of each topic stay free. Hands-on Selenium and API tasks follow their course lesson's own lock.

See Pro+

Already have Pro+? Sign in