• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Using "super" in Generics

 
Ranch Hand
Posts: 153
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

compilation error
The method add(capture-of ? super Horse) in the type List<capture-of ? super Horse> is not applicable for the arguments (GenAnimal)

I have learned that method add(List<? super Horse> arr) can add any thing that is super type of Horse, ok. I am adding "GenAnimal" which is super type of Horse, what made compiler unhappy

can any body please expain ?
 
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This means that you can pass a generic list of type Horse or super class of Horse. This does not mean that you can add super class object to this list.
 
Ranch Hand
Posts: 400
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
by saying thisyou mean the list can accept horse type of object or a object of which super type is horse, take a look on following code.


Hope this helps.

Minhaj

[Edit: spelling mistakes correct and added following]
Furthermore, before posting any question here please take a look on HowToAskQuestionsOnJavaRanch guide link.
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



point out the difference between the two you will understand

 
Malatesh Karabisti
Ranch Hand
Posts: 153
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From K & B Book

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."

I think my source of confusion is these words still these words are not very clear to me ?
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Malatesh Karabisti wrote:From K & B Book

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."

I think my source of confusion is these words still these words not very clear to me ?



its confusing.......because it doesnot work in that way.......can anybody comment
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public void addAnimal(List<? super Dog> animals)

this means that any subtype of dog can be accepted

like
Class whitecolor exends Dog{}
class Blackcolor extends Dog{}
class Brown extends Dog....

all three are accepted.......
anything which is having supertype as Dog is accepted......


can anybody comment if i am wrong
 
Minhaj Mehmood
Ranch Hand
Posts: 400
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Malatesh Karabisti wrote:From K & B Book
"Hey compiler, please accept any List with a generic type that is of type Dog, or a super type of Dog.
Nothing lower in the inheritance tree can come in, but anything higher than Dog is OK."



Yes, in other words its written: please accept any list with a generic type that is of type Dog or a list which super type is dog (the child of dog).
 
Malatesh Karabisti
Ranch Hand
Posts: 153
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From K & B Book
"Hey compiler, please accept any List with a generic type that is of type Dog, or a super type of Dog.
Nothing lower in the inheritance tree can come in, but anything higher than Dog is OK."


ok logically these words are correct with respect to method signature but not with add() method of the list. In fact this becomes inverse with add method since it can take only objects sub type or class which has been mentioned in right hand side of the super keyword(Incase any thing wrong please correct me).

class GenAnimal { }
class Horse extends GenAnimal { }
class WhiteColorHorse extends Horse { }

public class TestGeneric {
public static void main(String[] args) {
List<WhiteColorHorse> arr=new ArrayList<WhiteColorHorse>();
add(arr); // #1 error
}

private static void add(List<? super Horse> arr) {
arr.add(new Horse()); // this ok no error
arr.add(new WhiteColorHorse()); // this ok no error
arr.add(new GenAnimal()); //#2 error
}

}

#1 error The method add(List<? super Horse>) in the type TestGeneric is not applicable for the arguments (List<WhiteColorHorse>)

#2 error The method add(capture-of ? super Horse) in the type List<capture-of ? super Horse> is not applicable for the arguments (GenAnimal)
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Malatesh Karabisti wrote:From K & B Book

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."

I think my source of confusion is these words still these words are not very clear to me ?



Of course the method can accept any supertype of Dog or Dog itself. We get the error when we are adding elements to the list (List).

Regards,
venu.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Venu Chakravorty wrote:

Of course the method can accept any supertype of Dog or Dog itself. We get the error when we are adding elements to the list



can you please tell this? how.....
 
Venu Chakravorty
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
consider a case of using <? extends Animal>
here the method can accept anything that is an Animal and you can't use the add() method in this case.

consider a case of using <? super Animal>
here the method can accept anything that is a supertype of Animal. That might include Vertibrates, LivingThings or even Object. So the method can accept all these and you can add elements which are (IS-A) Animal(s).

Please correct me if I am wrong.

regards,
venu.
 
Sheriff
Posts: 9691
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Malatesh please Use Code Tags when you post a source code. That way your code looks formatted. Unformatted code is hard to read. You can add code tags by wrapping your code in [code] [/code] tags. You can edit your message using button and then add code tags to it...
 
Bartender
Posts: 543
4
Netbeans IDE Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
there is a special rule that allows such code under very specific circumstances in
which the code can be proven to be safe. This rule, known as wildcard capture,
allows the compiler to infer the unknown type of a wildcard as a type argument
to a generic method.

http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf Chapter 9, Page 20


If you make the list a List<GenAnimal> and you pass it with <? super GenAnimal>, it will work.

But you can only add subtypes of the lowest type, so the compiler is sure that everything you're adding is safe.

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic