• 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
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

K&B 2-minute drill generics question: Contradiction

 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It might be something I am not understanding here:

On page 609 at top, it is mentioned: "An ArrayList<Animal> can accept references of type Dog, Cat, or any othe subtype of Animal".

On page 609 at the bottom, the following is mentioned:
"List<Animal> aList=new Array List<Dog>() // no"

Why "no"?

Isn't Dog a subtype of Animal as mentioned on the top of page 609?
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The dot point in question explains this: "Polymorphic assignments applies only to base type, not the generic type parameter". Go back through the section on Polymorphism and Generics from page 582.
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Section 3. Generics and Subtyping.
 
Firas Zuriekat
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But on page 610, the following is mentioned:

"The polymorphic assignment rule applies everywhere an assignement can be made. The following isn't allowed: void foo(List<Animal> aList){}". Is there an example to see when it's not allowed?

Isn't this in contradiction to (page 609): "List<Animal> aList=new Array List<Dog>() // no"

They both talk about polymorphic assignement. But the one on page 609 result in compilor error? Or again the author is just talking about polymorphism when s/he says "no"?

I know Polymorphic assignments applies only to base type, not the generic type parameter. But I will read further to see if the "no" translates to "compilation failure" or "runtime exception" or "polymorphism not applicable".
[ May 31, 2006: Message edited by: Firas Zureikat ]
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic