• Post Reply Bookmark Topic Watch Topic
  • New Topic

Casting Runtime Exception Interface  RSS feed

 
Sven Sylta
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I cannot figure out why one of the down-castings causes a runtime-error and the other causes a complie-error. I have read that some down-castings will not cause a compile-error because it *could* be the right type. And if it's not, a ClassCastException is thrown.
I my example, I don't see the difference. Can somebody explain this?



 
Paweł Baczyński
Bartender
Posts: 2074
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IX is an interface-type reference. Such reference can be cast without compiler error to every class (well... except final classes that don't implement that interface).

First cast:
You are casting IX reference to type B. B does not implement IX but some class that extends B could do that. Change B definition to final and you will see compile time error.

Second cast:
This cast is legal at compile time. You are getting compiler error because you are trying to cast variable a which does not exist!
 
Sven Sylta
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:IX is an interface-type reference. Such reference can be cast without compiler error to every class (well... except final classes that don't implement that interface).

First cast:
You are casting IX reference to type B. B does not implement IX but some class that extends B could do that. Change B definition to final and you will see compile time error.


Great explanation.

Pawel Pawlowicz wrote:Second cast:
This cast is legal at compile time. You are getting compiler error because you are trying to cast variable a which does not exist!


I changed the variable to a3 and that caused a runtime-exception. Why is it not a complie-error instead since B and C are different branches. Same principle as the first casting?
 
James Harte
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, your primary compile problem is that variable a does not exist (different variable names), but let me give you a little more info:

In the first example where you have a runtime error, the compiler sees that there is a variable x which is of type IX which is an interface. It does not know what class it belongs to. When you have in this code variable x being casted to class B it checks and finds that the variable x is an instance of class D and, by extension, class A and, by extension, class A and that it additionally implements IX. These have no relationship to B so therefore you get a runtime error.

In the second example you have an instance of B saved as an instance of A, which is fine. However, you then try to do a casting of variable a to class C. This is an invalid relationship, but it too would be caught a runtime.

Essentially, when you cast a variable, you are telling the compiler to trust you that you know that the relationship is valid. In both of these situations, the relationship was not valid.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sven Sylta wrote:I changed the variable to a3 and that caused a runtime-exception. Why is it not a complie-error instead since B and C are different branches. Same principle as the first casting?

Even though it seesm very obvious to you, the compiler doesn't know that a3 is referring to an object of type B.
The only thing it takes into account is the type of a3 (which is A) and as casting an A to a C could work, there's no compiler error.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:Even though it seesm very obvious to you, the compiler doesn't know that a3 is referring to an object of type B.
The only thing it takes into account is the type of a3 (which is A) and as casting an A to a C could work, there's no compiler error.

@Sven: And I think you could help yourself out a lot by coming up with real names for your classes and variables. With names like A, B, and IX, you're constantly having to shift back and forth to work out how the names are connected.

Suggestion: Try something like ILifeformAnimalDogBloodHound → ... (or something completely different ). I think you'll be able to work things out for yourself a lot more easily.

Winston
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!