This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes Using Interface as Type Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Using Interface as Type" Watch "Using Interface as Type" New topic
Author

Using Interface as Type

naro pad
Ranch Hand

Joined: May 07, 2008
Posts: 58

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

Joined: Jan 28, 2008
Posts: 5575

nCopies return CopiesList[see java.util.Collections] which implements List .
Ram Narayan.M
Ranch Hand

Joined: Jul 11, 2010
Posts: 247

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...


SCJP 6 [SCJP - Old is Gold]
naro pad
Ranch Hand

Joined: May 07, 2008
Posts: 58

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

Joined: Jan 28, 2003
Posts: 4181
    
  21

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.


Steve
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using Interface as Type