Java super constructor3/2/2023 ![]() ![]() ![]() What would be the output if Object is replaced by Circle in the Test class using the Circle class in (a) and (b), respectively? The output would be true for (a), because circle1.equals(circle2) matches circle1.equals(Circle object) exactly in this case. Method overriding follows dynamic binding (determined by the actual type), but method overloading is always determined by the declared type. ![]() circle1 and circle2 have different addresses, leading to "false" output. In (a), method equals(Object o) is used at both compile time and run time. 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. The Circle class overrides the equals(Object o) method defined in the Object class. The output is true if the Circle class in (b) is used. (Note that either the declared type for circle1 and circle2 is Object would cause circle1.equals(circle2) to match circle1.equals(Object circle) by the compiler. At compile time, circle1.equals(circle2) is matched to equals(Object o), because the declared type for circle1 and circle2 is Object. The Circle class has two overloaded methods: equals(Circle circle) defined in the Circle class and equals(Object o) defined in the Object class, inherited by the Circle class. The output is false if the Circle class in (a) is used. Suppose that circle1.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. The Circle class in (a) and (b), respectively? If Object is replaced by Circle in the Test class, what would be the output to run Test using Show the output of running class Test with the Circle class in (a) and in (b), respectively. 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). 11.29 When overriding the equals method, a common mistake is mistyping its signature in the subclass. ![]()
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |