Analyze the following code. // Program 1: public class Test { public static void
ID: 3593656 • Letter: A
Question
Analyze the following code.
// Program 1:
public class Test {
public static void main(String[] args) {
Object circle1 = new Circle();
Object 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) {
Object circle1 = new Circle();
Object circle2 = new Circle();
System.out.println(circle1.equals(circle2));
}
}
class Circle {
double radius;
public boolean equals(Object circle) {
return this.radius ==
((Circle)circle).radius;
}
}
Question 10 options:
A) Program 1 displays true and Program 2 displays true B) Program 1 displays false and Program 2 displays true C) Program 1 displays true and Program 2 displays false D) Program 1 displays false and Program 2 displays falseExplanation / 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
---------------------------------------------------------
Hope this explination helps you still confused please comment .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.