File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generic question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generic question " Watch "Generic question " New topic
Author

Generic question

adil zahir
Greenhorn

Joined: May 09, 2013
Posts: 27


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 ?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18104
    
  39

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
adil zahir
Greenhorn

Joined: May 09, 2013
Posts: 27
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
Sheriff

Joined: Sep 28, 2004
Posts: 18104
    
  39

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

Joined: May 09, 2013
Posts: 27
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
Sheriff

Joined: Sep 28, 2004
Posts: 18104
    
  39

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

Joined: May 09, 2013
Posts: 27
thnaks Henry Wong , i understand what you mean .
Siddharth Raja
Greenhorn

Joined: Nov 11, 2013
Posts: 18
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
Sheriff

Joined: Sep 28, 2004
Posts: 18104
    
  39

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

Joined: Nov 11, 2013
Posts: 18
"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
Sheriff

Joined: Sep 28, 2004
Posts: 18104
    
  39

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
Sheriff

Joined: Sep 28, 2004
Posts: 18104
    
  39

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

Joined: Nov 11, 2013
Posts: 18
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 agree. Here's the link: http://aspose.com/file-tools
 
subject: Generic question
 
Similar Threads
Problems with Lists
Type-safe generic code doubt
about adding object to List<? super X>
Generic typing confusion in polymorphism
generics