• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics Question

 
Theodore David Williams
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please treat 'code' as pseudo code, hopefully I communicated my intent, thanks!








My question is, am I doing this right??? Is there any way using generics that I can ensure the downcast from Animal to Dog or Cat will work. I.E. I would like a compile time error in the second case. Any ideas??

Thanks
 
Kevin Workman
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Theodore David Williams wrote:Please treat 'code' as pseudo code, hopefully I communicated my intent, thanks!
My question is, am I doing this right??? Is there any way using generics that I can ensure the downcast from Animal to Dog or Cat will work. I.E. I would like a compile time error in the second case. Any ideas??

Thanks


You could use the instanceof keyword to check the type of Animal you're getting back from the getAnimal method. It doesn't help you at compile time, but it might help you avoid the runtime error.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50168
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You won't get a compile-time error because that was permitted in pre-Java-5 code. You ought however to get compiler warnings about both those assignments.
 
Theodore David Williams
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope no warning in:


The only warning is in the down cast in the get animal method


This is because getAnimalByName returns an animal type and the downcast generates a warning.
 
Rob Spoor
Sheriff
Pie
Posts: 20659
64
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look up what type erasure means, and you'll see why your code will never work.
 
Theodore David Williams
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code does work, I am just worried that the user of animal house (when writing code) will not be able to tell if he/she is making a mistake. Is there any way around this. Is my code 'bad' practice, it there better practice?

here is the quick example written in java that compiles and runs:






 
Rob Spoor
Sheriff
Pie
Posts: 20659
64
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Theodore David Williams wrote:

Trust me, that line makes your code broken. You cast to E, not knowing if the object actually is an instance of E. You also can't check. That's why you're getting a warning on the cast.
As a result, you are trying to treat your Cat as a Dog, and it will not cause a compiler error. You want the "Dog m = animalHouse.findByName("molly");" line to return null instead of throw an exception, I'd think.

There is one way to achieve this - with java.lang.Class:
 
Theodore David Williams
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob!

BTW, I guess I thought by

Your code will never work


You meant that it would not run/compile.

I do realize my code is 'broken' in a sense that it may not be up to par. I am still learning and have a ways to go, but realizing there is probably a better way to do it and asking for help on this forum is probably a good start....at least I think so

I truly do appreciate your help/ideas/comments....it makes me a much better coder!!!
 
Rob Spoor
Sheriff
Pie
Posts: 20659
64
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Theodore David Williams wrote:Thanks Rob!

BTW, I guess I thought by

Your code will never work


You meant that it would not run/compile.

It will not run, or at least it's not guaranteed it will run. In your last example you had to catch the ClassCastException because of this.

I do realize my code is 'broken' in a sense that it may not be up to par. I am still learning and have a ways to go, but realizing there is probably a better way to do it and asking for help on this forum is probably a good start....at least I think so

I truly do appreciate your help/ideas/comments....it makes me a much better coder!!!

In the end, what you want is actually not uncommon. It could have worked too, if Java didn't need to be backwards compatible. That's the main (only?) reason type erasure exists. If the generic type would have been included in byte code you could have done this, because the generic type would be available at runtime and things like "x instanceof E" would be possible. I even came up with a nice idea on how to change generics to require a specific constructor in a generic type. But again, the generic type needs to be available at runtime for that. In the end, Java's long history is biting us in the behind when it comes to generics.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic