• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

Generics Help please !

 
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The below gives me error :


However , this doesnt give me error :



Why so, could someone please explain ?
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


For primitives, the compiler knows that Dog is a subtype of Animal, and that Dog[] is a subtype of Animal[], for generics this is not the case.

As to why java allows implicit casting of array subtypes, which can then cause the error shown, I am not sure.
 
Ranch Hand
Posts: 310
1
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jose chiramal wrote:The below gives me error :





Hi Jose,

First of all your code (1st) will NOT give compile error, it gives a RuntimeException though!
The reason is you are trying to put a Dog() into a Cat[]. Dog is NOT a Cat , so that gives an Exception. It compiles fine because both are sub classes of Animal

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You also can to do:



In this method you can add all objects that extends Animal
 
Rajeev Rnair
Ranch Hand
Posts: 310
1
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Flavio Luiz Maria wrote:You also can to do:



In this method you can add all objects that extends Animal


Hi Flavio, welcome to the ranch!
yes, you are right! you can add all subclasses of Animal to the above method. So you can add a Dog() or a Cat()

hope this helps!

 
Ryan Hamilton
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why does java allow implicit casting of array subtypes?
Particularly when it doesn't allow similar behavior for collections?

 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is because there is an Exception for arrays--ArrayStore Exception while Generics are only a compile time protection with no run time exception.

That is, the line

List<Number> l=new ArrayList<Number>();
is only checked at compile time. At run time it becomes

List l=new ArrayList();

And if you are allowed to add a Float into Integer list at compile time just imagine what will happen to your code at run time
 
Get me the mayor's office! I need to tell her about this tiny ad:
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic