Question: 5. When overriding the equals method, a common mistake is mistyping its signature in the subclass. For example, the equals method is incorrectly written as



5. When overriding the equals method, a common mistake is mistyping its signature in the subclass. For example, the equals method is incorrectly written as equals(Circle circle), as shown in (a) in following the code; instead, it should be equals(Object circle), as shown in (b). Show the output of running class Test with the Circle class in (a) and in (b), respectively. public class Test public static void main(String args) { Object circle1 = new Circle(); Object circle2 = new Circle(); System.out.println(circle 1.equals(circle)); ) } (a) class Circle double radius; public boolean equals(Circle circle) { return this.radius == circle.radius; } } (b) class Circle { double radius; public boolean equals(Object 0) { return this.radius == ((Circle)o).radius; } } If Object is replaced by Circle in the Test class, what would be the output to run Test using the Circle class in (a) and (b), respectively? Suppose that circle 1.equals(circle2) is replaced by circle1.equals("Binding"), what would happen to run Test using the Circle class in (a) and (b), respectively? Reimplement the equals method in (b) to avoid a runtime error when comparing circle with a non-circle object. * 15
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
