• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

Generics doubt (? super String)

 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 ]
 
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<? 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.
 
Ranch Hand
Posts: 2410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An Object isn't necessarily a String.
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 )
 
raja kanak
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 558
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops. You are right. Now I am clear. Thanks friends.
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Keith Lynn
Ranch Hand
Posts: 2410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Parag P Kulkarni wrote:hi,
But still its throwing compiler error. How would I print what is added?



 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Wink, wink, nudge, nudge, say no more, it's a tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!