• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics doubt K&B

 
Guru dhaasan
Ranch Hand
Posts: 126
Java Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

In K&B book, p:595 the following code list resides

public class TestwildCards{
public static void main(String arg[]){
List<Integer> mylist=new ArrayList<Integer>();
Bar bar=new Bar();
bar.doInsert(mylist);
}
}

class Bar{
void doInsert(List<?> list){
list.add(new Dog());
}
}

It is given that the above code will not compile because, Dog object is added to an Integer list

in p:596 we have the following:

public class TestwildCards{
public static void main(String arg[]){
List<Integer> mylist=new ArrayList<Integer>();
Bar bar=new Bar();
bar.doInsert(mylist);
}
}

class Bar{
void doInsert(List<Object> list){
list.add(new Dog());
}
}

It is mentioned as this code listing compiles fine.


My Question is:

How the second list compiles while the first one fails ??


Thanks

Regards
Guru
 
anil kumar
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Guru


See this is the statement from K&B
-------------------------------------------------------------------
This time, class Bar, with the doInsert() method, compiles just fine.
------------------------------------------------------------------

Only Bar class Compiles.But when you try to compile TestwildCards class you will compile time error.


Thanks

Anil Kumar
 
Cor Takken
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guru,

The 'secret' is the typed parameter: if a wildcard is specified with a collection as a parameter you can't add elements to the collection passed. As soon as the wildcard is replaced by a "concrete" type (in this case a <Object>) you can add. It all has to do with the compiler being able to guarantee that what is passed is compatible with what you are trying to add to the collection (type-safety). It is explained quite well in chapter 7.

Cor
[ June 12, 2007: Message edited by: Cor Takken ]
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guru,


public class TestwildCards{
public static void main(String arg[]){
List<Integer> mylist=new ArrayList<Integer>();
Bar bar=new Bar();
bar.doInsert(mylist);
}
}

class Bar{
void doInsert(List<Object> list){
list.add(new Dog());
}
}

It is mentioned as this code listing compiles fine.


This code doesn't compile. List<Object> can only take a List parameterized
with Object, nothing else. Polymorphism applies to base type only
and not to the parameterized type.

It could be ArrayList<Object> or LinkedList<Object> or Vector<Object>.
You can see that the base type can change, not the parameterized type.
It is <Object> only.


Thanks,
 
Guru dhaasan
Ranch Hand
Posts: 126
Java Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Anil
 
Guru dhaasan
Ranch Hand
Posts: 126
Java Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Cor
 
Guru dhaasan
Ranch Hand
Posts: 126
Java Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Chandra
 
Guru dhaasan
Ranch Hand
Posts: 126
Java Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

supposing I have the following code:

public class TestwildCards{
public static void main(String arg[]){
List<Integer> mylist=new ArrayList<Integer>();
Bar bar=new Bar();
bar.doInsert(mylist);
}
}

class Bar{
void doInsert(List<Object> list){
list.add(new Dog());
}
}


Will this compile or not ???

Sincd Polymorphism applies to base type only and not to the parameterized type. Here the base type was not changed but the parameterized type changes

Will the Dog object be added into the Integer list???


Also what is the difference does it make between List<?> and List<object> ???


Thanks,
Guru
 
anil kumar
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Guru

List<Integer> mylist=new ArrayList<Integer>();

Here the base type is List and parameterised type is Integer.
So there is no problem with the statement.

void doInsert(List<Object> list){
list.add(new Dog());
}


The above method takes only a List<Object> ,It won't take any other.
--------------------------------------------------------------------------
Also what is the difference does it make between List<?> and List<object> ???
-------------------------------------------------------------------------

List<?> :Takes a list of any type ,it may be List<Float>or List<Animal> and .............

List<Object>:It takes List<Object> only.

Thanks

Anil Kumar
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guru,

Actually in that code problem is not in adding Dog object to the List<Object>.
The problem is in method call, bar.doInsert(mylist);.


Also what is the difference does it make between List<?> and List<object> ???


If you substitute the List<Object> with List<?> in your code, the method
call is OK but you can't add anything to the List because it will become
read only.

List<?> means you can pass a List (or its subtypes) parameterized
with any type to the method. Remember you can't add anything to the List
then.


Thanks,
 
Guru dhaasan
Ranch Hand
Posts: 126
Java Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Anil
 
Guru dhaasan
Ranch Hand
Posts: 126
Java Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Chandra
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic