aspose file tools*
The moose likes Java in General and the fly likes Generics Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Generics Question" Watch "Generics Question" New topic
Author

Generics Question

Theodore David Williams
Ranch Hand

Joined: Dec 21, 2009
Posts: 102
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

Joined: Sep 28, 2010
Posts: 151
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

Joined: Oct 13, 2005
Posts: 39436
    
  28
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

Joined: Dec 21, 2009
Posts: 102
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

Joined: Oct 27, 2005
Posts: 19723
    
  20

Look up what type erasure means, and you'll see why your code will never work.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Theodore David Williams
Ranch Hand

Joined: Dec 21, 2009
Posts: 102
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

Joined: Oct 27, 2005
Posts: 19723
    
  20

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

Joined: Dec 21, 2009
Posts: 102
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

Joined: Oct 27, 2005
Posts: 19723
    
  20

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generics Question