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.