File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using Interface as Type

 
naro pad
Ranch Hand
Posts: 58
Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nCopies return CopiesList[see java.util.Collections] which implements List .
 
Ram Narayan.M
Ranch Hand
Posts: 247
Chrome Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • 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
Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • 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

 
Steve Luke
Bartender
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic