• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

k&b: generics ? syntax

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.util.*;
class Apple{}
class mac extends Aplle{
public static void main(String[] munch){
List<Apple>a=new ArrayList<Apple>();
basket(a);
}
//insert code here
}


One of the options in the answers is

static void basket(List<?>list){list.add(new Apple());}

This option is given as incorrect because add() method is not compatible with the ? syntax.

Can someone elaborate this for me?
 
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

import java.util.*;
class Apple{}
class mac extends Aplle{
public static void main(String[] munch){
List<Apple>a=new ArrayList<Apple>();
basket(a);
}
//insert code here
}


One of the options in the answers is

static void basket(List<?>list){list.add(new Apple());}

This option is given as incorrect because add() method is not compatible with the ? syntax.

Can someone elaborate this for me?



Hi Nisha,
When you use "?" while declaring you cannot add anything to that. So always remember when "?" is used while declaring you cannot add anything to it.



Regards
Nik
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
list is of type List<?> and that means list can reference a list-object of type List<String>, of type List<Number> or even a list-object of type List<Thread>. It can reference every list-object you want it to.

Now you go and say: "hey compiler, I am going to add an apple into the list-object referenced by list". No wonder, the compiler won't let you, because he cannot be sure, if that operation is type-safe. If you are absolutely sure, that the list-object referenced by list is indeed able to hold apples, than you have to tell the compiler that. You could do so with an explicit cast, like:

((List< ? super Apple > ) list).add(new Apple())

telling him, "the object in heap that list is referring to is a list-Object of Apples or maybe Fruits, I am not sure, but I am sure, that it can hold apples"

[ May 22, 2007: Message edited by: Sasha Ruehmkorf ]
[ May 22, 2007: Message edited by: Sasha Ruehmkorf ]
 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey nik,

I dont understand why this is wrong? Can you please eloborate more..!



The bolded part in the above code, says that any thing can be passed as an argument.

The rest of the code takes care of it by adding only Apple to it.

Every thing seems good. Where is the problem?? Can any body help?
 
Prasad Tamirisa
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks sasha,

I am clear with it now.
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to add to what Nik has said:


In generic methods, there are two instances at which you cannot add anything to the passed in parameter within the method.

1. when the generic type parameter has just the wild care '?'. (which is your eg.)
2. Also when there is an upper bound "? extends someClass"

static void basket(List<? extends Apple> list){
list.add(new Apple("Red Delicious"));
}
In these two cases, the compiler cannot be sure what will be added to the list therefore not permitted.


But you can add, if the parameter uses super in the method argument.
(? super someClass) --> lower-bound
eg:
static void basket(List<? super Apple> list){
list.add(new Apple("Red Delicious"));
}



Also, if you are not adding anything inside the method, then the compiler will let you go away with it.

The following will compile fine:

[ May 22, 2007: Message edited by: M Krishnan ]
 
Nisha Pinjarkar
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot guys !
 
Nisha Pinjarkar
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On page 596(k&b book)

List <? super Animal > dList = new ArrayList<Dog>();

Now, this is wrong as Dog is too low in class hierarchy. Only <Animal> or <Object> is legal.

With this explanation, why is this wrong?

static void basket(List<? super Apple>list){list.add(new Object());}

WHy can't we add Object ? isn't it the super type of Apple?
 
Ranch Hand
Posts: 329
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Nisha Pinjarkar:
static void basket(List<? super Apple>list){list.add(new Object());}

WHy can't we add Object ? isn't it the super type of Apple?



Think about this:

List<? super Apple> can refer either to List<Object> or List<Apple>. Because of the second, the compiler cannot guarantee that List.add(Object) will work.

In fact, the compiler yields:


Which means that add(Object) method is not defined for List<? super Apple> .
[ May 22, 2007: Message edited by: Sergio Tridente ]
 
The human mind is a dangerous plaything. This tiny ad is pretty safe:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic