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

Using Interface as Type

 
Ranch Hand
Posts: 58
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am in a confusion and I want your help. First before continue with my problem, I clearly understand the meaning of Object, Class, Data Type and Interface. When we create a Collection we declare a variable of Interface type and then assign an instance of a class that implements that interface. For example:


Up to this point I can understand. I recently saw a sample of code that didn't follow that rule and it was working fine and I want someone to explain it to me. The code was:


The Collections.nCopies() method returns a List Type Object. Which List is an Interface isn't it? How this can work?

I don't know it might be I am a little confused.

Thanks
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
nCopies return CopiesList[see java.util.Collections] which implements List .
 
Ranch Hand
Posts: 247
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For instance Car object which have the name instance variable "Lancer"... The requirement is list of 10 identical Lancer cars...

Using nCopies(int n, Object o), it can be done...

Internally it returns an instance whose class implements List interface. You can see the internal coding part of nCopies by extracting src.zip which is shipped along with JDK package...
 
naro pad
Ranch Hand
Posts: 58
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok know I understand... it returns a List of type AbstractList which is implement the List interface.

Thank you guys...I should have be check it before post the question

 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

naro pad wrote:
The Collections.nCopies() method returns a List Type Object. Which List is an Interface isn't it? How this can work?



It returns some instance of some unknown class which implements List. The internal workings, such as if it uses a CopiesList or a class which derives from AbstractList is irrelevant and can not be relied upon because it isn't part of the API. The API says it returns a List, anything more is an implementation detail which can be changed at any time. And that is the point - you code versus the List interface so that in some future release when a different implementation is used you aren't affected at all - you don't have to change your code because the internal working of nCopies had changed. That's why it is good to use interfaces as return types in your code as well - so when you decide to change the behavior of you method (say you find a better, quicker, or safer implementation) then you don't have to re-write the method AND the code which calls it.
 
There were millions of the little blood suckers. But thanks to this tiny ad, I wasn't bitten once.
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