Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem in Generics

 
Poornima Sharma
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends,

in Generics what does it mean

<X extends Object>

I know what <? extends Object> mean but i cant understand the above.
 
ramesh maredu
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
X extends Object, is syntax used to define boundaries, this kind of syntax is used in generic classes and methods.

See below code.



commented lines don't compile if you remove comment
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well both <? extends Object> and <X extends Object> mean the same thing to a certain extent. The only difference being this

with <X extends Object> you can declare references of type X.

<X extends Object> void method(List<X> list, X obj)
{
list.add(obj);
}

but you can't do the same with <? extends Object>

void method(List<? extends Object> list, ? obj) //compilation error.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic