aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics doubt (? super String) Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Generics doubt (? super String)" Watch "Generics doubt (? super String)" New topic
Author

Generics doubt (? super String)

raja kanak
Ranch Hand

Joined: Oct 18, 2006
Posts: 135

The above code breaks at //1
Object is super class of String, then why it is not compiling?
[ March 16, 2007: Message edited by: raja kanak ]

live
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
<? super String> means we can only add Strings

if you add Object.
It seems something like this,
String s = new Object();

Hope you got it.


Thanks & Regards,<br />T.Srinivasan,<br />SCWCD 1.4(89%),SCJP 5.0(75%)<br />"That service is the noblest which is rendered for its own sake." - Mahatma Gandhi
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2341
An Object isn't necessarily a String.
Amit Wadhwaa
Ranch Hand

Joined: Feb 15, 2007
Posts: 74
Originally posted by Srinivasan thoyyeti:
<? super String> means we can only add Strings

if you add Object.
It seems something like this,
String s = new Object();

Hope you got it.


Shouldn't the above code mean that we can add any Super class of String?

Shouldn't this be the interpretation
Object o=new String(); //this is legal

rather than the other way?

(I have to go thru the chapter again I think )


SCJP 5 94%<br /><a href="http://amit-wadhwa.blogspot.com/" target="_blank" rel="nofollow">My Blog</a>
raja kanak
Ranch Hand

Joined: Oct 18, 2006
Posts: 135
K&B Chapter 7: Generics and Collections, page number 594 says

public void AddAnimal(List<? super Dog> animals) is essentially, "Hey compiler, please accept any List with a generic type that is of type Dog, or a supertype of Dog. Nothing lower in the inheritance tree can come in , but anything higher than Dog is OK."


Im confused Can any body throw some light on this?
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2341
public void AddAnimal(List<? super Dog> animals) is essentially, "Hey compiler, please accept any List with a generic type that is of type Dog, or a supertype of Dog. Nothing lower in the inheritance tree can come in , but anything higher than Dog is OK."


This means that the method can accept a List<Dog>, List<Animal>, or List<Object>, but within the method you are only allowed to add an instance of a Dog or a subclass of Dog to the list.
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi Raja,

Accepting and Adding are two different issues.

public void addAnimal(List<? super Dog> animals)

addAnimal can accept List of animals or Dogs.

But you can only add Dogs to that List "animals".

animal.add(new Animal()) //is wrong;

Please go through the Book it was clearly mentioned in the Book whats the concept behind these compile time checks.

raja kanak
Ranch Hand

Joined: Oct 18, 2006
Posts: 135
Oops. You are right. Now I am clear. Thanks friends.
Chris Stann
Ranch Hand

Joined: Oct 10, 2006
Posts: 49
List<? super String> l1 = new ArrayList<Object>();

Please clarify. What is the point of <? super String> in List reference declaration if I can't add anything to it other than a String? The book does not seem to give an answer.


EXCEL IN ALL YOU DO
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2341
In the case of String, you can only add String objects because String is a final class, and can't have subclasses.

But in a situation where a class can have subclasses, you can use the lowerbound to make additions to the collection type safe. The compiler only allows you to add classes that are the lower bound or have an "is a" relationship with the lower bound.

For example in the method

public static void add(List<? super Animal> list) { ... },

you can add an Animal instance or an instance of a subclass of Animal because the compiler knows that the only parameterized Lists you can send are List<Animal> or List<Object>, assuming Animal doesn't extend another class.

But if you have the method

public static void add(List<? extends Animal> list) { ... },

you are not allowed to add anything to the list because the compiler can't guarantee type safety.

Animal could have many subclasses and each one of them could have subclasses.

Remember that the "is a" relationship is not symmetric.
[ March 16, 2007: Message edited by: Keith Lynn ]
Chris Stann
Ranch Hand

Joined: Oct 10, 2006
Posts: 49
Keith, thank you for your explanation - I now have a good understanding of method type safety.

But I still don't understand why you would want to create a List this way:

List<? super Integer> l1 = new ArrayList<Object>();
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2341
Originally posted by Chris Stann:
Keith, thank you for your explanation - I now have a good understanding of method type safety.

But I still don't understand why you would want to create a List this way:

List<? super Integer> l1 = new ArrayList<Object>();


I'm not sure in practice how often you would do this.

But on mock exams these types of questions are used to test the concepts to make sure that we have a deep understanding of the concepts even though we may not use all of them.
Parag P Kulkarni
Greenhorn

Joined: Nov 18, 2009
Posts: 1
hi,

I understand the explanations and thank you for that; but I am still confused about one thing....

I have a function "addString(List<? super String> myList) and inside I am adding few String to myList. But when I try to traverse through the list (using enhanced for loop "for (List s: myList)", I am getting compiler error stating following
Type mismatch: cannot convert from element type capture-of ? super String to String

I also tried to use ArrayList like below


But still its throwing compiler error. How would I print what is added?
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Parag P Kulkarni wrote:hi,
But still its throwing compiler error. How would I print what is added?


Dragos Nica
Ranch Hand

Joined: Oct 25, 2009
Posts: 39
Parag P Kulkarni wrote:hi,

I understand the explanations and thank you for that; but I am still confused about one thing....

I have a function "addString(List<? super String> myList) and inside I am adding few String to myList. But when I try to traverse through the list (using enhanced for loop "for (List s: myList)", I am getting compiler error stating following
Type mismatch: cannot convert from element type capture-of ? super String to String

I also tried to use ArrayList like below


But still its throwing compiler error. How would I print what is added?



You get the compilation error, because "myList" reference has type List<? super String>, this means that your method addString can get as a parameter a list of String or a list of supertype of String.
It is not important that inside the method you create an instance of type ArrayList<String> and myList is pointing to it, because the compiler is looking what is the type of myList and it is List<? super String>, so as far as compiler is concerned <? super String> is not <String>

You have two posibilities:
- use a reference of type Object inside enhanced for to iterate through myList elements.
- inside addString method use a different reference not myList.


SCJP 6.0 (88%)
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Generics doubt (? super String)
 
Similar Threads
generics Strike again !!!
Generics
Linked List equals method
GENERICS
Collections-Getting error, using subList(fromIndex, toIndex) in List!