• 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

Generics doubt

 
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
See the foolowing code below

public class Animal
{
public static void main(String[] args)
{
Animal an=new Animal();
List<Object> lstobj=new ArrayList<Object>();
List<? extends Animal> lstani=new ArrayList<Animal>();
lstani=an.display(lstobj>;
System.out.println(lstani);

}

public <E extends Animal> List <? extends Animal> display(List lst)
{
lst.add(new Object());
return lst;
}

}
class Dog extends Animal
{
}


My doubts is

public <E extends Animal> List <? extends Animal> display(List lst)

In the above method declaration it can accept whatever extends Animal but why is it accepting a Object as argument.


Thanks All
 
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

nik

change your method this

public <E extends Animal10> List <? extends Animal10> display(List<E> lst)
{
lst.add(new Object());
return lst;
}

Check the bold words, you will come to know why is it accepting?

Thanks
Anil Kumar
[ May 14, 2007: Message edited by: anil kumar ]
 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

change your method this

public <E extends Animal> List <? extends Animal10> display(List<E> lst)
{
lst.add(new Object());
return lst;
}



Hi Anil i changed and tested but i am not understanding the behaviour of the method why it works when its changed. So can you explain me?
 
anil kumar
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey

nik

------------------------------------------------------------------------
public <E extends Animal> List <? extends Animal> display(List lst)
-------------------------------------------------------------------------

<E extends Animal>

what does it mean ?

You are saying to the compiler that , the elements in the list are of subtypes of Animal type.

Ok

Up to this point is ok

But you have to say to list also.Which you did not?


display(List lst)// line 1

There is no restriction,that the list must contain the elements which are subtypes of Animal.

ok

if you declare like this

display(List<E> lst)

Then the compiler will check what is <E> here.Which you defined eariler.

i think it is clear

Thanks

Anil Kumar
 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anil,
Look at the below statement

public <E super Animal> List <? super Animal> display(List lst)
-------------------------------------------------------------------------

Why the above statement wont compile?
 
anil kumar
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey

nik


check this once

click here
[ May 15, 2007: Message edited by: anil kumar ]
 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anil,
I have looked at that document i am not getting
 
reply
    Bookmark Topic Watch Topic
  • New Topic