• Post Reply Bookmark Topic Watch Topic
  • New Topic

Casting Exception in Generics  RSS feed

 
Neeraj jain
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have studied that Generics are used to shift the Class Cast Exception into Compile time errors , So that we get errors at compile time error and we do correct them before executing ,
but Here is a program in which i am getting Class Cast Exception



// Getting Exception at line no 29 which i know why it occurs but just wanna ask that isn't it should be caught at compile time According to Generics ?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neeraj jain wrote: I have studied that Generics are used to shift the Class Cast Exception into Compile time errors , So that we get errors at compile time error and we do correct them before executing ,
but Here is a program in which i am getting Class Cast Exception

// Getting Exception at line no 29 which i know why it occurs but just wanna ask that isn't it should be caught at compile time According to Generics ?



While Generics does help with implicit casting -- there isn't really much it can do with explicit casting. Explicit casting means that you are overriding the compiler -- so unless the compiler can guarantee that it is impossible for the cast to succeed, it will have to allow it at compile time.

Henry
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. The compiler checks that only Animal objects are put into the Store and retrieved from it - that is the case. It can not ensure anything about types other than Animal (like Cat or Dog). By using that cast *you* are telling the compiler that the object is, in fact, a Dog. But the compiler will (and can) only ensure that it is an Animal.

Try using String instead of Cat or Dog in lines 28 and 29 and see what happens.
 
Heena Agarwal
Ranch Hand
Posts: 262
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neeraj jain wrote:

I have studied that Generics are used to shift the Class Cast Exception into Compile time errors , So that we get errors at compile time error and we do correct them before executing ,
but Here is a program in which i am getting Class Cast Exception

.......

// Getting Exception at line no 29 which i know why it occurs but just wanna ask that isn't it should be caught at compile time According to Generics ?


Generics by itself cannot prevent a ClassCastException.

When you code this -->



all you are doing is creating a Store reference that is parameterized with an Animal type. So the set method takes an Animal as an argument.

Since Cat is-a Animal, you can invoke set as follows.



Since Dog is-a Animal, you can invoke set as follows also.


always returns an Animal.
It could be a Cat or a Dog or an Animal or anything that is-a Animal.

In your case, st.get() returns an Animal type and you have no way of knowing whether it is a Cat or a Dog or any other Animal subtype.

All the generics tell you is that you are going to get an Animal. So if you coded as follows, for instance..



you will get a compiler error cause the compiler knows that st.get() can only return you an Animal.
When you try to cast it to a Dog, the compiler knows that Dog is-a Animal and trusts that you know you are casting the Animal object to its right sub-type. It trusts you to know what you're doing cause it has no information at compile time about the actual object.

But the Java run time has that information. It knows that Animal out there is a Cat and hence it can't be cast to a Dog. Thus you get the ClassCastException.

Remember the parameterized types by themselves cannot prevent the ClassCastExceptions. Nor were they meant to do so.





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