| Author |
Generics
|
James Tharakan
Ranch Hand
Joined: Aug 29, 2008
Posts: 580
|
|
KB chapter7 question 6
If the below change is made,
then ,its again a error,because
Wolf class breaks because its munch(Sheep)method no longer fulfills the contract of Carnivore.
So does that mean that if one class interprets E as a particular type,then EVERY OTHER class should interpret E as the same type? ?
|
SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
|
 |
Punit Singh
Ranch Hand
Joined: Oct 16, 2008
Posts: 952
|
|
class Sheep extends Plant,
But
class Wolf extends Animal implements Carnivore<Sheep>
that means Carnivore<Sheep>==Carnivore<E extends Plant>.
But contract says that
interface Carnivore<E extends Animal> extends Hungry<E> {}.
So , it is not valid.
|
SCJP 6
|
 |
James Tharakan
Ranch Hand
Joined: Aug 29, 2008
Posts: 580
|
|
|
ok..got it
|
 |
Punit Singh
Ranch Hand
Joined: Oct 16, 2008
Posts: 952
|
|
James just concentrate on this line
interface Carnivore<E extends Animal> extends Hungry<E> {}
1) You can see Carnivore type parameter is : E extends Animal
2) Now our case:
class Sheep extends Plant implements Carnivore<Wolf>
You can see here: Sheep extends Plant, so Sheep is a Plant now.
3) Now Wolf contract.
class Wolf extends Animal implements Carnivore<Sheep>
you can see here : Carnivore<Sheep>
and Sheep is now Plant as in the case 2.
so it becomes Carnivore<Plant>.
Fine but case 1 contract says Carnivore should take Animal or subtype of Animal.
interface Carnivore<E extends Animal>
So this case is not possible, as it is breaking the contract of interface Carnivore<E extends Animal>
|
 |
James Tharakan
Ranch Hand
Joined: Aug 29, 2008
Posts: 580
|
|
Thankyou... thing are clear now...
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
Just to make it easier to understand, I have modified the code a bit
|
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
|
 |
James Tharakan
Ranch Hand
Joined: Aug 29, 2008
Posts: 580
|
|
One more question from the book.
Given a method declared as
public static <E extends Number> List<E> process(List<E> nums)
// INSERT DECLARATIONS HERE
output = process(input);
they say that the following statement cannot be used,because The return type of process is definitely declared as a List, not an ArrayList
ArrayList<Integer> input = null;
ArrayList<Integer> output = null;
I thought, since ArrayList implements List,why cant we use this. isn't it something like List<Integer> fun= new ArrayList<Integer>();
|
 |
Punit Singh
Ranch Hand
Joined: Oct 16, 2008
Posts: 952
|
|
ArrayList<Integer> input = null;
this line is possible as it will make
List<Integer> nums=input;(means new ArrayList<Integer> input=null;)
ArrayList<Integer> output = null;
this is not possible as process() function return type is List.
so output=process() will result in
ArrayList<Integer> output = reference type of List<Integer>;
And subclass reference cannot be assigned superclass reference without casting.
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
The return type of process method is List not ArrayList. So the return will look like this
ArrayList<Integer> output = new List<Integer>();
This is not allowed as List cannot be cast to ArrayList without an explicit cast.
|
 |
James Tharakan
Ranch Hand
Joined: Aug 29, 2008
Posts: 580
|
|
|
Ohh... I am sorry guys for such a silly question.Generics and collection are confusing me a lot. :evil: .
|
 |
Punit Singh
Ranch Hand
Joined: Oct 16, 2008
Posts: 952
|
|
|
Go slowly James, bump is ahead.
|
 |
 |
|
|
subject: Generics
|
|
|