Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Casting and Compiler Errors

 
Warren Weis
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been studying for the OCA test off and on for the last month and I'm trying to understand how the compiler catches class cast errors. I think I understand the rules for assigning objects to object references, but I'm not so sure about assigning objects to interface references. Here is an example of what I mean.
It makes sense to me that the compiler could catch that I cannot cast an Integer to a Doable, because Integer does not implement Doable.
But why can't the compiler catch that my class Black does not implement Doable either?


 
Henry Wong
author
Marshal
Pie
Posts: 22111
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Warren Weis wrote:
But why can't the compiler catch that my class Black does not implement Doable either?


Because a Black reference refers to something that IS-A Black instance (which includes sub-classes to the Black class). And it is possible for a sub class to implement the Doable interface.

Henry
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Warren Weis wrote:
But why can't the compiler catch that my class Black does not implement Doable either?


Because a Black reference refers to something that IS-A Black instance (which includes sub-classes to the Black class). And it is possible for a sub class to implement the Doable interface.

Henry

I think you missed a bit off Henry

... whereas Integer is a final class and so can't be extended and so the compiler knows there are no subclasses that will implement Doable.

If you make the Black class final you will get an error on that line as well.
 
Warren Weis
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just saw someone else's post, but I think the final bit is the key.
Because Integer is a final class, there is no possibility that a derived class of Integer could implement the interface. I added a final modifier to the Black class and the compiler does catch the cast error now.

Thanks.


 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3817
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think the answer is as straight forward.

If b were an instance variable, it would have been possible for b to start pointing to an instance of a subclass of Black that implements Doable between those two lines due to multithreading. However, since b is a local variable, there is no way b can point to any other instance between those two lines. I think it is possible for a compiler to catch this. But Java compiler doesn't and I think the reason is they tried to strike a balance between how far a compiler should go into run time's territory.

The core issue here is this: Out of all the information that is available at compile time, what should be used by the compiler to flag errors. In this particular case, it is a judgement call of the compiler designers, imho.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you already discovered: the final keyword is key. You can find a detailed explanation in this thread (it's about instanceof operator but as you will see casting & instanceof are related...).
 
Henry Wong
author
Marshal
Pie
Posts: 22111
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:
I think you missed a bit off Henry

... whereas Integer is a final class and so can't be extended and so the compiler knows there are no subclasses that will implement Doable.

If you make the Black class final you will get an error on that line as well.


Sorry for misreading. For some reason, I related the Integer class with the Double class... and saw it as not being in the same hierarchy. Weird, because I saw it correctly with the Black class....

Henry
 
Warren Weis
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all the quick replies. This is my first post to this forum and it has been very helpful.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic