GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics doubt K&B Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generics doubt K&B" Watch "Generics doubt K&B" New topic
Author

Generics doubt K&B

Guru dhaasan
Ranch Hand

Joined: Sep 13, 2006
Posts: 126

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


Thanks, Shiv
SCJP, OCE - JSP & Servlets Developer
anil kumar
Ranch Hand

Joined: Feb 23, 2007
Posts: 447
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

Joined: May 21, 2007
Posts: 58
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 ]

SCJP 1.5
Dolphins are grey, but they dream in colour.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
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,


cmbhatt
Guru dhaasan
Ranch Hand

Joined: Sep 13, 2006
Posts: 126

Thanks Anil
Guru dhaasan
Ranch Hand

Joined: Sep 13, 2006
Posts: 126

Thanks Cor
Guru dhaasan
Ranch Hand

Joined: Sep 13, 2006
Posts: 126

Thanks Chandra
Guru dhaasan
Ranch Hand

Joined: Sep 13, 2006
Posts: 126

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

Joined: Feb 23, 2007
Posts: 447
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

Joined: Feb 28, 2007
Posts: 1707
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

Joined: Sep 13, 2006
Posts: 126

Thanks Anil
Guru dhaasan
Ranch Hand

Joined: Sep 13, 2006
Posts: 126

Thanks Chandra
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generics doubt K&B