• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

List Interface doubt

 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1557
5
Eclipse IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21497
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic