• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Generic question

 
Greenhorn
Posts: 29
Mac Java ME Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


why i can't do this

list1.add( new Animales)

we have

List< ? super Dog>

that mean we can add

Dog


Animales

and

Object


can you explain me why i have this Error ?
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

adil zahir wrote: we have

List< ? super Dog>

that mean we can add

Dog


Animales

and

Object


can you explain me why i have this Error ?



Can you explain to us why you think that a List<? super Dog> should be able to add Dog, Animales, and Object? ... because that is not true.

Henry
 
adil zahir
Greenhorn
Posts: 29
Mac Java ME Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Can you explain to us why you think that a List<? super Dog> should be able to add Dog, Animales, and Object? ... because that is not true.

Henry



yes that is not true we can't add

Animales

and

Object

but we can add

Dog



so for that i want knew why ?!!! because we have

List<? super Dog>

that mean evrything super Dog
 
Henry Wong
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

adil zahir wrote:
so for that i want knew why ?!!! because we have

List<? super Dog>

that mean evrything super Dog



I think you are under the misconception that a List<? super Dog> is a List that can take anything that is Dog or super class of Dog. That is not true.

A List<? super Dog> is a list of some wildcard generic type that is Dog or super class of Dog. This means that it could be List<Dog>, List<Animales>, or List<Object>. It is not a list of all three types combined. It is a list of one of those generic types, and the compiler doesn't know which it is.

This means that the compiler will only allow an operation that can satisfy all three types. And since a Dog IS-A Dog, IS-A Animales, and IS-A Object, it is allowed to be added. The other two class types doesn't satisfy the condition.

Henry
 
adil zahir
Greenhorn
Posts: 29
Mac Java ME Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Henry Wong,

what i understand we can't do

add.( new Animales() )


because the object " new Animales() " not satisfied " the 3 rules

x IS-A Dog, x IS-A Animales, and x IS-A Object

?
 
Henry Wong
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

adil zahir wrote:Henry Wong,

what i understand we can't do

add.( new Animales() )


because the object " new Animales() " not satisfied " the 3 rules

x IS-A Dog, x IS-A Animales, and x IS-A Object

?




Not 3 rules. It is 3 different generic types that matched, and hence, must be satisfied for the add() operation. With different wildcards, there could be much more than three types that can match.

Henry
 
adil zahir
Greenhorn
Posts: 29
Mac Java ME Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thnaks Henry Wong , i understand what you mean .
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is allowed:
List<Animal> L = new ArrayList<Animal>();
L.add(new Dog()); L.add(new Animal());

This is NOT Allowed:
List<? super Dog> L = new ArrayList<Animal>();
L.add(new Dog());
L.add(new Animal()); //COMPILATION ERROR!

now i understand that due to 'type erasure' JVM does not have info of what type of List its dealing with, so the compiler has to be strict. But here the compiler knows the ArrayList is of type 'Animal' since we are initializing it that way.

But here's a twist....later on in the code someone could reassign L as follows: L = new ArrayList<Dog>(); (the compiler will have to permit this since L is <? super Dog>)
now L.add(new animal()) is obviously going to blow up the prog since L is of type Dog
SO, im guessing that this is the main reason that L.add(new Animal()) is being disallowed.....possible conversion (by re assigning) to Dog (subtype)

Am i correct?
 
Henry Wong
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Siddharth Raja wrote:This is allowed:
List<Animal> L = new ArrayList<Animal>();
L.add(new Dog()); L.add(new Animal());

This is NOT Allowed:
List<? super Dog> L = new ArrayList<Animal>();
L.add(new Dog());
L.add(new Animal()); //COMPILATION ERROR!

now i understand that due to 'type erasure' JVM does not have info of what type of List its dealing with, so the compiler has to be strict.



Java Generics type erasure refers to the lost of type between compile time and runtime. This discussion is completely about compile time.


Siddharth Raja wrote: But here the compiler knows the ArrayList is of type 'Animal' since we are initializing it that way.

But here's a twist....later on in the code someone could reassign L as follows: L = new ArrayList<Dog>(); (the compiler will have to permit this since L is <? super Dog>)
now L.add(new animal()) is obviously going to blow up the prog since L is of type Dog
SO, im guessing that this is the main reason that L.add(new Animal()) is being disallowed.....possible conversion (by re assigning) to Dog (subtype)

Am i correct?



There is nothing in the Java Language Specification that mentions any determination of the type by its usage.... so, there is no "the compiler knows the ArrayList is of type 'Animal' since we are initializing it that way" or anything related to the "twist".

Henry
 
Siddharth Raja
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"There is nothing in the Java Language Specification that mentions any determination of the type by its usage"

i'm talking about compile time
so then why are these wrong?
List<? extends Dog> L = new ArrayList<Animal>();
or
List<String> L = new ArrayList<Integer>();
and so on...

Since the discussion is only about compile time....the compiler should check which 'type' right?

also my basic point was this (lets forget type erasure and everything else irrelevant): since there's a possibility that L = new ArrayList<Dog>(); can be done later, thereby reassigning L, there's a danger of letting objects of type Animal get added to L. the compiler 'anticipates' this and doesn't let Animal objs get added to the List.

Am i correct in assuming this?
If not then why? whats wrong with this point please?


 
Henry Wong
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Siddharth Raja wrote:"There is nothing in the Java Language Specification that mentions any determination of the type by its usage"

i'm talking about compile time
so then why are these wrong?
List<? extends Dog> L = new ArrayList<Animal>();
or
List<String> L = new ArrayList<Integer>();
and so on...



Well, obviously, there is type checking for the assignment -- but that is it. It is still a wildcard generic list after the assignment -- there is no additional detail that is kept after the assignment.

Henry
 
Henry Wong
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Siddharth Raja wrote:
also my basic point was this (lets forget type erasure and everything else irrelevant): since there's a possibility that L = new ArrayList<Dog>(); can be done later, thereby reassigning L, there's a danger of letting objects of type Animal get added to L. the compiler 'anticipates' this and doesn't let Animal objs get added to the List.

Am i correct in assuming this?
If not then why? whats wrong with this point please?



What do you mean by "anticipates"? How does a compiler anticipates? It is just following the exact definition of the specification.... perhaps you are speculation on why it was defined that way? That detail is not available in the JLS.

And BTW, there is nothing wrong with your point -- especially if it helps you remember how it works. Just keep in mind that the compiler follows exact specifications, explanations of scenarios with a twist, or anticipations is speculation on why the specification was written that way. For that detail, you need to ask someone who designed the language.

Henry
 
Siddharth Raja
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes it does help me remember it :-)
'anticipates', 'speculates'...just an attempt to personify the compiler, of course...all this to make stuff simpler. its easier to imagine this way
Thanks for the confirmation though.
 
I think he's gonna try to grab my monkey. Do we have a monkey outfit for this tiny ad?
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic