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

List Interface doubt

 
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1558
5
Eclipse IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henry
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Today you are you, that is turer than true. There is no one alive who is youer than you! - Seuss. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic