All mock tests are FREE till 31 December — register to save your progress

Class Good has a field m and a method public boolean equals(Good n) { return n.m == m; }. In main: Good m1 = new Good(22), m2 = new Good(22); Object s1 = new Good(22), s2 = new Good(22); then it prints m1.equals(m2), s1.equals(s2), m1.equals(s2), s1.equals(m2). What is printed?

Pick an option to check yourself, or open the answer below.

Show answer & explanation

Correct answer: D. True, False, False, False C + + f(

Explanation

equals(Good) overloads rather than overrides Object.equals. Only m1.equals(m2) (both static type Good) calls it and gives true. The other three resolve to Object.equals, which compares references: false.

More Programming Languages and Computer Graphics questions