My question is, reference type is "? super Integer", then it should be able to accept Object, as Object is a super type of Integer?
thanks
Harvinder
But when i cast the (Integer) new Object(). then it work fine.
My question is, reference type is "? super Integer", then it should be able to accept Object, as Object is a super type of Integer? Am I correct?
Originally posted by victor kamat:
Because of bad code presentation here it is again: ...
No. You are not correct. "? super Integer" does *not* mean that it can accept any type that is a super of Integer. It means that it accepts an unknown type that is a super of Integer. So, in order to add to the list, you must add the unknown type -- and the only types that can be any unknown type of super of Integer, is Integer, as an Integer IS-A all super types of Integer.
List <? super Dog> listDogs = new ArrayList<Animal>(); //wildcard with super then listDogs is allow to reference type of Animal (super class of dog)
listDogs.add(new Dog());//Dog passed Is-an Animal test
listDogs.add(new Puppy());//Puppy passed Is-an Animal test
//fine as Puppy is subclass of Dog
listDogs.add(new God());//God failed Is-an Animal test
List <? extends Dog>; listDogs = new ArrayList<Animal>(); //Compiler error, because listDogs trying to reference ArrayList with type Animal, but Animal failed Is-A Dog test //Statement 1
So, List<? extends Dog> listDogs = new ArrayList<Puppy>();//is a valid assignment, because listDogs trying to reference ArrayList with type Puppy and Puppy passed Is-A Dog test //Statement 2
listDogs.add(new Dog());// compiler error? refer Statement 2? but it passed the Is-A Dog test
listDogs.add(new Puppy());// compiler error? refer Statement 2? but it passed the Is-A Dog test
For *? extends* wild card the compiler does'nt know to what level down the class hierarchy of class *Dog* the actual collection type can belong to. So it's even more dangerous in case of *? extends* to allow somebody to even add to the collection.
Then what can be added into the list?
thanks
Harvinder
Originally posted by Harvinder Thakur:
[QB]
If i define a collection of as List<? super Dog>; listDogs
then i am telling the compiler that *listDogs* can be assigned any reference of a generic collection which contains objects of type Dog or super type of Dog. So, if i have the following classes:
then i can say,
List <? super Dog> listDogs = new ArrayList<Animal>();
or
List <? super Dog> listDogs = new ArrayList<Object>();
list.add(new Dog());
list.add(new Puppy());
I am free to add any Dog or a subclass of Dog object to it because the compiler knows that no matter what, the Dog or its subclass object is going to pass the IS-A test for the type (actually a supertype of Dog) of objects stored in the actual collection ( which i am actually modifying i.e. the ArrayList of Animals or Objects in this case).
But, i cannot add a super type of Dog into *listDogs* the reason being the following scenario:
SCJP 1.6, SCWCD 5.0, SCBCD 5.0 [loading..]
SCJP 1.6, SCWCD 5.0, SCBCD 5.0 [loading..]
Originally posted by Himalay Majumdar:
So, Does it mean this is possible
listDogs.add(new Animal())
if NOT.. then what does Super type imply
thanks
Harvinder
Originally posted by Edmen Tay:
List<? super AA> lst1 = new ArrayList<AA>();//lst1 can contain AA,BB,CC,DD and unknown type that is super type of AA
List<? super BB> lst2 = new ArrayList<BB>();//lst2 can contain BB,CC,DD and unknown type that is super type of BB
List<? super CC> lst3 = new ArrayList<BB>();//lst3 can contain CC,DD and unknown type that is super type of CC
List<? super DD> lst4 = new ArrayList<CC>();//lst4 can contain DD and unknown type that is super type of DD
thanks
Harvinder
SCJP 1.6, SCWCD 5.0, SCBCD 5.0 [loading..]
Originally posted by Himalay Majumdar:
I dont want to sound silly. Looking at your post i feel <? extends AA> should do wat <? super AA> is doing. And there should be nohing like <?super AA> existing
thanks
Harvinder
<? super AA> implies that lst1 can be *assigned* AA or its SuperType. BUT, you can only add AA or it's SubTypes to lst1 because that is the only addition which guarantees typesafety and therefore the compiler gives thumbs up.
<? extends AA> implies that the list can be *assigned* AA or its SubTypes. BUT, you can add nothing to it since if you were allowed to add an object to it then the typesafety boasted to be given by Generics will be GONE! So, consider it that adding generics to the java programming language had to bring about such vagaries and complications just to make your code typesafe.
SCJP 1.6, SCWCD 5.0, SCBCD 5.0 [loading..]
CLUCK LIKE A CHICKEN! Now look at this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
|