Ask 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 trueB) Program 1 displays false and Program 2 displays trueC) Program 1 displays true and Program 2 displays falseD) Program 1 displays false and Program 2 displays false

+1
Answers (1)
  1. 18 August, 20:30
    0
    B) Program 1 Displays false and Program 2 Displays true

    Explanation:

    equals method is defined in Object class and every class is by default extends Object class.

    In order to use equals method of object class, subclass must extends equals method of Object class.

    Equals method always take Object class reference as a parameter.

    So in Program one, equals method's parameter is of type Circle which means Our subclass did't override the equals method of Object class, and default equals method of object class compares the memory addresses that's why program 1 returned false.

    In program 2, equals method is properly override the Object class equals method. That's why when we try to call equals method, it basically executed subclass equals method and that's why it returned true as double default value is 0.0;
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Analyze the following code./ / Program 1:public class Test {public static void main (String[] args) {Object circle1 = new Circle (); Object ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers