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

Are List<Object> and List<? extends Object> the same thing?

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
They both can accept a reference type of any Object or subclass right?
 
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes..you are right.

But only List<Object> allows you to add elements.
 
Robert O'Leary
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Himalay Majumdar wrote:Yes..you are right.

But only List<Object>allows you to add elements.



Cool...and if you were using them as a method parameters, only List<? extends Object> can accept an argument other than List<Object>e.g as in Line 5.

 
Himalay Majumdar
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
:deleted:
 
Himalay Majumdar
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Robert O'Leary wrote:
Cool...and if you were using them as a method parameters, only List<? extends Object> can accept an argument other than List<Object>e.g as in Line 5



Not true.. List<? extends Object> can accept ANY argument..including..List<Object>



Check out this link for more practice
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Himalay,

I think you and Robert are saying the same thing. Maybe there is a little misunderstanding going on.

As for whether List<? extends Object> is the same as List<Object>, they are similar but quite different. You basically can't add anything to List<? extends Object>.

I however think that List<? super Object> and List<Object> are exactly the same. If I'm mistaken, someone will correct me.
 
Himalay Majumdar
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ruben..how are you doin buddy.

Robert mentioned..List<? extends Object> can accept an argument other than List<Object>.

but List<? extends Object> can accept List<Object>

I dont see any difference between List<? super Object> and List<Object>,

but List<? super Dog> and List<Dog> are not same.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Himalay Majumdar wrote:Hi Ruben..how are you doin buddy.

Robert mentioned..List&lt;? extends Object&gt; can accept an argument other than List&lt;Object&gt;.

but List&lt;? extends Object&gt; can accept List&lt;Object&gt;

I dont see any difference between List&lt;? super Object&gt; and List&lt;Object&gt;,

but List&lt;? super Dog&gt; and List&lt;Dog&gt; are not same.




List<? super Object> is very similar to List<Object>

but given Foo extends Object

List<? super Foo> list = new ArrayList<Object>(); is legal
List<Foo> list = new ArrayList<Object>(); is not legal

also for method calls

public void doStuff(List<Foo> l)
{
l.add (new Foo()); //Legal
}

public void doStuff(List<? extends Foo> l)
{
l.add (new Foo()); //Not legal
}

public void doStuff(List<? super Foo> l)
{
l.add(new Foo()); //Legal
}

It's really confusing but.. good luck
 
Himalay Majumdar
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Adding to Rex's code





Thats the reason I said

List<? super Object> is SAME as List<Object>,

but List<? super Dog> and List<Dog> are NOT SAME.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Himalay Majumdar wrote:Hi Ruben..how are you doin buddy.

Robert mentioned..List&lt;? extends Object&gt; can accept an argument other than List&lt;Object&gt;.

but List&lt;? extends Object&gt; can accept List&lt;Object&gt;

I dont see any difference between List&lt;? super Object&gt; and List&lt;Object&gt;,

but List&lt;? super Dog&gt; and List&lt;Dog&gt; are not same.


Himalay,

Doing good man, how's the studying going?

I think Robert didn't mean that List<? extends Object> can't accept Object references, I think he meant that the only difference between List<? extends Object> and List<Object> is that List<? extends Object> can accept a reference of a type other than Object. But maybe it's me who misunderstood.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rex Tan wrote:
List<? super Object> is very similar to List<Object>


Hi Rex,

Actually I think they are exactly the same, because the only thing the wildcard can be is Object, since Object doesn't have a superclass. But for other classes, then List<? super A> would be different from List<A>. I think Himalay said the same thing also.
 
Himalay Majumdar
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah Ruben..I dint interpret his statement as whole... By "other than List<Object>" he means everything including List<Object>.
Now his code makes sense to me.

By the way..April 4th is my Judgement day..how about you.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Himalay Majumdar wrote:Yeah Ruben..I dint interpret his statement as whole... By "other than List&lt;Object&gt;" he means everything including List&lt;Object&gt;.
Now his code makes sense to me.

By the way..April 4th is my Judgement day..how about you.


I am sure you will do great, you have come a long way since the beginning of the journey.
I haven't set up a date yet myself, but it will be soon.
 
Himalay Majumdar
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much Ruben. Will keep you posted.
 
Robert O'Leary
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Himalay Majumdar wrote:

Robert O'Leary wrote:
Cool...and if you were using them as a method parameters, only List&lt;? extends Object&gt; can accept an argument other than List&lt;Object&gt;e.g as in Line 5



Not true.. List&lt;? extends Object&gt; can accept ANY argument..including..List&lt;Object&gt;



Check out this link for more practice



yep that was i saying, i could have been clearer though! thanks for the discussion gents
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic