aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes List Interface doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "List Interface doubt" Watch "List Interface doubt" New topic
Author

List Interface doubt

saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

I am not getting scene behind set() here.I think set() is not a static method.There must be abstract declaration in either Collection interface or List interface so how set() is working here on List reference variable, from where it is getting definition ?


Tell the difficulties that i am difficult.
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

saloni jhanwar wrote:I am not getting scene behind set() here.I think set() is not a static method.There must be abstract declaration in either Collection interface or List interface so how set() is working here on List reference variable, from where it is getting definition ?


the set method is not static method. it is defined in list interface contract.
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

gurpeet singh wrote:
saloni jhanwar wrote:I am not getting scene behind set() here.I think set() is not a static method.There must be abstract declaration in either Collection interface or List interface so how set() is working here on List reference variable, from where it is getting definition ?


the set method is not static method. it is defined in list interface contract.


Can you tell me source of your statement ? , i didn't define method in interface till now so wondering.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1509
    
    5

saloni jhanwar wrote:i didn't define method in interface till now so wondering.

Well, set method is part of List interface. So, all the concrete implementations of List interface must implement set method.

Arrays.asList method returns an object of ArrayList - which is an inner class of Arrays (please note that this is java.util.Arrays$ArrayList, not java.util.ArrayList).

Now, this Arrays$ArrayList extends AbstractList, which implements List. Hence, Arrays$ArrayList has to have a concrete implementation of set method (which it does have).

I hope this helps.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Anayonkar Shivalkar wrote:
saloni jhanwar wrote:i didn't define method in interface till now so wondering.

Well, set method is part of List interface. So, all the concrete implementations of List interface must implement set method.

Arrays.asList method returns an object of ArrayList

I hope this helps.


It returns List not ArrayList, i have checked it, that's why i was confused here.
asList()
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1509
    
    5

saloni jhanwar wrote:It returns List not ArrayList, i have checked it, that's why i was confused here.

Yes, but as I said, Arrays$ArrayList does implement List - i.e. Arrays$ArrayList IS-A List. Hence, it is very much possible for a method to have return type as List, but returning anything which IS-A List.

Here is the actual code:


I hope this helps.
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

saloni jhanwar wrote:
Anayonkar Shivalkar wrote:
saloni jhanwar wrote:i didn't define method in interface till now so wondering.

Well, set method is part of List interface. So, all the concrete implementations of List interface must implement set method.

Arrays.asList method returns an object of ArrayList

I hope this helps.


It returns List not ArrayList, i have checked it, that's why i was confused here.
asList()



yes you are right . the api says that it returns list. but i think you are confused because the return type is just a reference. a reference to something. that reference can be of abstract or interface type. in this case it is of interface type list. it actually points to something that has implemented list interface, which means that implementation has to have set method definition.

i didn't understood what Anayonkar said about the inner class thing
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

gurpeet singh wrote:
saloni jhanwar wrote:
Anayonkar Shivalkar wrote:
saloni jhanwar wrote:i didn't define method in interface till now so wondering.

Well, set method is part of List interface. So, all the concrete implementations of List interface must implement set method.

Arrays.asList method returns an object of ArrayList

I hope this helps.


It returns List not ArrayList, i have checked it, that's why i was confused here.
asList()



yes you are right . the api says that it returns list. but i think you are confused because the return type is just a reference. a reference to something. that reference can be of abstract or interface type. in this case it is of interface type list. it actually points to something that has implemented list interface, which means that implementation has to have set method definition.

i didn't understood what Anayonkar said about the inner class thing


Yes i am also looking for that inner class in Arrays class document but i didn't find any yet.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1509
    
    5

saloni jhanwar wrote:Yes i am also looking for that inner class in Arrays class document but i didn't find any yet.

Check out the source code of Arrays class
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Anayonkar Shivalkar wrote:
saloni jhanwar wrote:Yes i am also looking for that inner class in Arrays class document but i didn't find any yet.

Check out the source code of Arrays class


You did so ? how do you know ?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

saloni jhanwar wrote:
gurpeet singh wrote:
saloni jhanwar wrote:
It returns List not ArrayList, i have checked it, that's why i was confused here.
asList()

yes you are right . the api says that it returns list. but i think you are confused because the return type is just a reference. a reference to something. that reference can be of abstract or interface type. in this case it is of interface type list. it actually points to something that has implemented list interface, which means that implementation has to have set method definition.

i didn't understood what Anayonkar said about the inner class thing


Yes i am also looking for that inner class in Arrays class document but i didn't find any yet.



That java.util.Arrays.ArrayList inner class is actually private to the java.util.Arrays class. Hence, there is no documentation for it (no javadoc, that is). It is also out of scope by the time the asList() method returns, so you can't use it to access any of the instances methods.... Of course, the java.util.List interface, which is implemented by that class, is public (and still in scope), which is why you should just use it as a List object...... In other words, it is just an implementation detail. Don't worry about it. Just know that it returns a List object.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Thanks Henry
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1509
    
    5

saloni jhanwar wrote:You did so ? how do you know ?

You can find source code in src.zip file (which comes as part of JDK).
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Anayonkar Shivalkar wrote:
saloni jhanwar wrote:You did so ? how do you know ?

You can find source code in src.zip file (which comes as part of JDK).

nice
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: List Interface doubt