Julie P Chen wrote:Can someone clarify?
Julie P Chen wrote:The explanation is particular confusing when it says "... Line 9 compiles correctly because they are related types..." I assume "they" are referring to "CanClimb" and "HasClaws". But the thing is that they are not related? HasClaws did not extends from CanClimb or vice versa.
Practice Exam 2, Explanation wrote:Line 7 compiles correctly since all objects can be implicitly cast to java.lang.Object, so option C is incorrect.
Practice Exam 2, Explanation wrote:Line 8 compiles without issue since CanClimb is a subclass of Object, so option D is incorrect.
Practice Exam 2, Explanation wrote:Line 9 also compiles correctly since they are related types, so option E is incorrect.
Practice Exam 2, Explanation wrote:At runtime, all of the casts succeed since Koala implements both CanClimb and HasClaws, so option F is incorrect. The code compiles and runs without issue, with 15true as the output.
Ramya Subraamanian wrote:Like in line4 and line5, you can cast the reference variable to a Interface, it compiles(provided the class is not final and Interfaces are not final). But it will throw a ClassCastException, if they are not related or compatible.
Julie P Chen wrote:We know that we can assign a sub-class reference type to an object, with an explicit cast, as in Line 2, because java.lang.Object is the super-class of all objects, including CanClimb.
So Line 2, "canClimb" has a CanClimb reference type, and to Java, it is referencing a CanClimb object, which compiles, because this newly created Koala object is also an instance of CanClimb.
Julie P Chen wrote:The confusion comes from Line 3 because "canClimb" is a CanClimb reference type, even though we know it is still referencing a Koala object.
So for "canClimb" to be cast to HasClaws reference type, it would only make sense that HasClaws and CanClimb interfaces would have to be related as it is in the Mock Exam Explanation - HasClaws implements CanClimb.
Julie P Chen wrote:Thus, this still seems like an error in the Mock Exam to me. Or, Line 3 could be a Compiler Error for casting un-related types, if it does not throw an ClassCastException at runtime.
Julie P Chen wrote:If I understand correctly, the principle is that we can cast a reference variable to any interface without compiler error (though, may throw CCE if they are un-related) because interface is inherently non-final.
Julie P Chen wrote:If that is the case, this example would also compile just fine if the interfaces (CanClimb and HasClaws) are replaced as abstract classes, since abstract classes are also inherently non-final.
[OCP 21 book] | [OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
[OCP 21 book] | [OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Julie P Chen wrote:"an interface can be CAST by ANY classes, including a class that extends Object in the question"?
Roel De Nijs wrote:
Jeanne Boyarsky wrote:Yes. For example this works.
For clarity, "it works" means "it compiles successfully, but throws a ClassCastException at runtime".
[OCP 21 book] | [OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
An elephant? An actual elephant. Into the apartment. How is the floor still here. Hold this tiny ad:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
|