98.0k views
5 votes
Analyze the following code. // Program 1: public class Test { public static void main(String[] args) { Object circle1 = new Circle(); Circle circle2 = new Circle(); System.out.println(circle1.equals(circle2)); } } class Circle { double radius; public boolean equals(Circle circle) { return this.radius == circle.radius; } } // Program 2: public class Test { public static void main(String[] args) { Circle circle1 = new Circle(); Circle circle2 = new Circle(); System.out.println(circle1.equals(circle2)); } } class Circle { double radius; public boolean equals(Object circle) { return this.radius == ((Circle)circle).radius; } }

User Cobp
by
5.3k points

1 Answer

3 votes

Answer:

The output is false if the Circle class in program 1 is used.

It has 2 overload methods :

equals(Circle circle) defined in the Circle class and equals(Object o) defined in the Object class, inherited by the Circle class.

At compile time, circle1.equals(circle2) is matched to equals(Object o), because the declared type for circle1 and circle2 is Object.

The output is true if the Circle class in program 2 is used.

The Circle class overrides the equals(Object o) method defined in the Object class.

At compile time, circle1.equals(circle2) is matched to equals(Object o) and at runtime the equals(Object o) method implemented in the Circle class is invoked.

Answer:

Program 1 displays false and Program 2 displays true

Step-by-step explanation:

User Dmzkrsk
by
5.4k points