• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

question on generics

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers,
I have a doubt in generics of K&B book, in page 594 its says
"When you use the <? Super �> syntax, you are telling the compiler that you can accept the type on the right hand side of super or any of its supertypes, since � and this is the key part that makes it work- a collection declared as any supertype of dog will be able to accept a dog as an element. List<Object> can take dog. List<animal> can take a dog and List<dog> can take a dog. So passing any of those in will work. So the super keyword in wildcard notation lets you have a restricted , but still possible way to add to a colection".
the code I tried is
import java.util.*;
public class GenericTest1 {
public static void main(String[] args)
{
List<animals11> l = new ArrayList<animals11>();
l.add(new dog());
l.add(new dog());
l.add(new dog());
System.out.println(l.size());
GenericTest1 gt = new GenericTest1();
gt.addanimals(l);

}
public void addanimals(List<? super dog> l)
{
l.add(new animals11());
System.out.println(l.size());
}
}
class animals11
{
public void getMe()
{
System.out.println("from animals11");
}
}
class dog extends animals11
{
public void getMe()
{
System.out.println("from dog");
}
}
as per my understanding when i am able to add the super type of dog object when i use the <? super dog> i should be able to add the animals11 object as it is a superclass of dog, but why am i getting the error when i am trying to add in addanimals method???.can anyone please explain.
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello MuraliH,

The Generic signature List<? super dog> will allow you to achieve polymorphism with generics so you can pass in a List<Dog>, List<Animal11> and List<Object> i.e Dog and anything up the inheritance tree but you can only add Dog or subclass(s) of Dog i.e Anything Down the inheritance tree. If you try to add a super class of Dog i.e Animal11 to the list, it translates to something like the following.

Animal11 a11 = new Animal11();
Dog d = a11 // you can not assign a super class object to a sub class ref.

Hope thats clear and helped
Kind regards.
Hasnain Javed Khan.
SCJP 5.
 
MuraliH Krishna
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Hasnain Javed Khan,
thanks for your help. its very clear now.
Thanks once again,
Murali
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic