Are List<Object> and List<? extends Object> the same thing?
Robert O'Leary
Ranch Hand
Joined: Mar 24, 2009
Posts: 41
posted
0
They both can accept a reference type of any Object or subclass right?
SCJP6
Himalay Majumdar
Ranch Hand
Joined: Sep 28, 2008
Posts: 324
posted
0
Yes..you are right.
But only List<Object> allows you to add elements.
SCJP 1.6, SCWCD 5.0, SCBCD 5.0 [loading..]
Robert O'Leary
Ranch Hand
Joined: Mar 24, 2009
Posts: 41
posted
0
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
Joined: Sep 28, 2008
Posts: 324
posted
0
:deleted:
Himalay Majumdar
Ranch Hand
Joined: Sep 28, 2008
Posts: 324
posted
0
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>
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.
All code in my posts, unless a source is explicitly mentioned, is my own.
Himalay Majumdar
Ranch Hand
Joined: Sep 28, 2008
Posts: 324
posted
0
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.
Rex Tan
Greenhorn
Joined: Mar 26, 2009
Posts: 8
posted
0
Himalay Majumdar wrote: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.
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
SCJP 1.6, SCWCD 1.5, MCTS, MCT
Himalay Majumdar
Ranch Hand
Joined: Sep 28, 2008
Posts: 324
posted
0
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
Joined: Dec 16, 2008
Posts: 1032
posted
0
Himalay Majumdar wrote: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.
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
Joined: Dec 16, 2008
Posts: 1032
posted
0
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
Joined: Sep 28, 2008
Posts: 324
posted
0
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
Joined: Dec 16, 2008
Posts: 1032
posted
0
Himalay Majumdar wrote: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.
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
Joined: Sep 28, 2008
Posts: 324
posted
0
Thank you so much Ruben. Will keep you posted.
Robert O'Leary
Ranch Hand
Joined: Mar 24, 2009
Posts: 41
posted
0
Himalay Majumdar wrote:
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>