0

Possible Duplicate:
Coding to interfaces?

I am reading the Collections tutorial from Java and it strongly recommends that codes referring to Collections be implemented using its interface type and not its actual implementation type. e.g:

Set<String> s = new HashSet<String>();

It says that it will give me flexibility to change implementations should I decide to change it later on.

Set<String> s = new TreeSet<String>();

Other than flexibility, are there any other benefits to implementing a collection using its interface type?

Community
  • 1
  • 1
SamIAm
  • 2,241
  • 6
  • 32
  • 51

3 Answers3

4

Yes, when using the interface class, you will only have access to the most default methods. These methods guarantee to be intuitive. When using the implementation class, you might see more methods that might confuse you or be abused.

For example: a collection interface will have a method that returns the number of elements, lets say: size(). But the implementation class might also provide a capacity() method that tells you how big the underlying array is.

But as the tutorial tells you, the most important reason is that you can change the implementation without any effort. Changing the implementation might be interesting for performance optimization is very specific cases.

Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
  • I forgot about how some methods can be abused. I was originally thinking on the lines that more choices are better but. I can see how that might cause some problems. – SamIAm Dec 06 '12 at 14:02
1

I think here it's more than just Collections, it's about polymorphism: better to use the interface as the declared type, because you may change implementation and/or concrete class to be bound at runtime. (discussion here could be longer - there are plenty of documents/tutorials about this - Java basics)

acostache
  • 2,177
  • 7
  • 23
  • 48
  • i think op knows that, the question is what are the other advantages apart from polymorphism :P – PermGenError Dec 06 '12 at 12:56
  • Imagine you find out that a particular implementation is better than the one you are using. By using the interface type as the declared type, you only need to change the type for the assignment, rather than changing the declared type everywhere you reference it. This makes the code more maintainable. – kahowell Dec 06 '12 at 13:00
1

The interface type often contains fewer methods than an actual implementation, making it tempting to use the later because it allows access to them.

However, this then ties your hands in a significant way. For example, if you decide to expose a return type of Vector from a public method of a class that uses a vector, but later realize that your module would be better served with a LinkedList, you now have some problems -- this will break anything which uses the method returning a vector.

On the other hand, if in the first place you had used a return type of List then there would be no problem -- you could switch the internal Vector to a LinkedList, or implement your own thing that fulfills interface List. In my experience, this is a common event (of course, if you make it difficult or impossible, then it will happen less).

So, unless you have a specific reason to do otherwise (eg, you need to provide access to methods only available with vectors), always use the generic interface type for your return value.

I realize this is still about flexibility but it is not clear from your post if you understand how important that is. If you are asking for a better reason to use interfaces, ie. "is it okay to use the specific implementation if I don't care flexibility", ie. "I don't care about flexibility and want to use specific types, is that okay?" the answer is you should care about flexibility ;) As others have said, this is an important fundamental of good java programming.

CodeClown42
  • 11,194
  • 1
  • 32
  • 67