Could someone tell me what my instuctor is talking about in this question?
Joined: Mar 12, 2012
I'm being asked to answer this question:
What would be a use for the instanceof operator? Obviously it will designate if it is "an instance of" something--that is not the answer. The question is, why would we want to know that?
I keep getting my answers rejected and this is his reason:
"Obviously it will designate if it is "an instance of" something--that is not the answer"
-is because this is not the information we want in your answer.
Let's look at your compareShapes() method-
"First you asked if both the shapes were instances of ComparableShape,
Then you cast those shapes to ComparableShape, so that you could call that interface's method: getArea().
Shapes that don't implement ComparableShape might not have a getArea() method, so we had to CAST our shapes to the interface before we called getArea. And *before* we could cast our shapes, we had to first use instanceof.
Calling getArea was the *reason* we used instanceof in this case.
Why else might we want to see if one object is an instance of another? I'm not looking for what instanceof does, I want to know the *reason* why you'd use it."
He keeps talking about that compareShape() method but I don't see anything that can help me answer it.
Forget about Java for a second, and think about root causes, and reasoning.
Consider this: You want to get a six pack of beer. You don't have any cash. So you go to the ATM to withdraw some money, then go to the store and buy some beer. So why did you go to the ATM? Yes, what you did at the ATM was to get cash, but you didn't really go to the ATM for the cash, that was just the means to an end. What you wanted was beer, and to get the beer you needed the cash, which you got from the ATM. So why else might you need to go to the ATM?
Translate that into what the prof was saying about instanceof and ComparableShape. You want to compare the areas of two Shape objects, but Shape does not have a getArea() method, but ComparableShape does. You need to call the getArea() method, so you need to cast the Shape to a ComparableShape. But you can't do that unless you know the Shape is an instance of ComparableShape - after all, not all Shapes are ComparableShapes. So before you do the cast, you first check if (shape instanceof ComparableShape) (or at least that sounds like the prof. expected you to do).
So to repeat the causal chain in the same way I did the beer story: You want to get the area of a Shape, but only ComparableShapes are known to have an area. So you do shape instanceof ComparableShape and if it is true, you cast the Shape to a ComparableShape and call the getArea() method. So why did you do the instanceof comparison? Sure, you did it to check if the Shape was an instance of a ComparableShape, but that was just a means to an end. What you really wanted to do was safely call a method which ComparableShape has that Shape does not. So why else would you want to use instanceof?
Don't think about what the proximal effect of the instanceof operator, think of how you could use that effect to get something done.