7

Possible Duplicate:
What does it mean to “program to an interface”?

I noticed that some people like to declare an object as one of the interfaces it implements even though, within the scope of the variable, it is not necessary to look at it as the interface, e.g. there is no external API that expect an interface.

For example:

Map<String, Object> someMap = new HashMap<String, Object>();

Or you can just do

HashMap<String, Object> someMap = new HashMap<String, Object>();

and avoid importing java.util.Map altogether.

What are the advantages of declaring it through an interface (first above) as opposed to the class itself (second above)?

Thanks

Community
  • 1
  • 1
amphibient
  • 29,770
  • 54
  • 146
  • 240
  • 1
    This doesn't seem like a duplicate to me since it is asking a specific question about a small scope, whereas the mentioned question is about programming to an interface in general. In this case, is there an advantage to programming to an interface when the scope of the variable involved is very small (only within a method body and not exposed otherwise). – digitaljoel Oct 09 '12 at 18:11
  • actually the same question has been asked many times, all in the form of `X var = new Y()`, i.e. on a small cope. of course, the correct answer was never chosen:) – irreputable Oct 09 '12 at 18:15

5 Answers5

3

An interface such as Map<A, B> declares what an object can do. On the other hand, a class such as HashMap<A, B> defines how an object does what the interface declares it does.

If you declare a variable (or field, or whatever) a Map<A, B>, you are stating that your code depends only on the contract defined by that interface, it does not depend on the specifics of an implementation.

If you declare it a HashMap<A, B>, it is to be understood that you need that specific version of a map (for whatever reason), and it cannot be replaced for something else.

I tend to dislike the common answers like "because you can change the implementation" simply because, after years and years of practice, you will see that it won't happen as frequently as that, and the major benefits are really this subtle (but clear) expression of your intents.

Bruno Reis
  • 37,201
  • 11
  • 119
  • 156
  • i know the difference between a class and an interface, that was not the question. please refer to OP – amphibient Oct 09 '12 at 18:01
  • @foampile, I'm sorry, but, if you really, really, really know and understand the difference, you'd not have asked the question. – Bruno Reis Oct 09 '12 at 18:03
  • i just edited the OP highlighting "even though, within the scope of the variable, it is not necessary to look at it as the interface" – amphibient Oct 09 '12 at 18:06
  • 2
    Yes, I've seen it, and my answer still stands. There's absolutely no difference in usage in your case, the only difference is the subtleties you are communication through your choice. – Bruno Reis Oct 09 '12 at 18:07
  • @BrunoReis.. Sorry but I strongly disagree with your last paragraph.. – Rohit Jain Oct 09 '12 at 18:07
  • @BrunoReis.. This is always a good design to use interface as reference type, so that you don't have to expose your implementatino to the outside world.. And hence, the user can change the implementation according to his/her need, yet adhering to the existing system definition.. He won't need to the change any code for that.. – Rohit Jain Oct 09 '12 at 18:09
  • "subtleties you are communication through your choice" -- huh? and what would that choice be? – amphibient Oct 09 '12 at 18:09
  • @BrunoReis.. On the other hand, if you are designing to an implementation.. Then if you happen to change your requirement, then you will have to modify existing system.. – Rohit Jain Oct 09 '12 at 18:10
  • 2
    @foampile: the choice between the two possibilities you are asking about. – Bruno Reis Oct 09 '12 at 18:10
  • @BrunoReis -- and which choice did i make? i don't recall making one – amphibient Oct 09 '12 at 18:11
  • 2
    "contract" is not everything, an implementation also cares about performance, memory etc. the programmer had a reason to chose HashMap, not another Map. then why does he wants to hide that fact in the code? – irreputable Oct 09 '12 at 18:11
  • 2
    @foampile, that's the whole point: you didn't, you are asking "what should I chose?", or "when should I use Map<...> and when should I use HashMap<...>?". I'm answering as: the choice should be made based on what you want to communicate to others (including your future self when rereading the code some time later). – Bruno Reis Oct 09 '12 at 18:13
3

Since it's not part of API, it is implementation detail. It's better to be specific in implementations, there's no point to be abstract here.

my prev answer

Use interface or type for variable definition in java?

Community
  • 1
  • 1
irreputable
  • 44,725
  • 9
  • 65
  • 93
1

If the variable is not used later, there is no advantage/disadvantage. The reason you would use an interface rather than an object is to allow for more flexibility, but if that variable isn't used, there is no difference from a performance persepective.

Igor
  • 33,276
  • 14
  • 79
  • 112
1

If you use Map<String, Object> someMap, you are designing to an interface rather than implementation.. So you can switch between other implementation easily..

So, your Map can point to a HashMap, LinkedHashMap, or any other object, that is a subclass of Map.

So, if you have: -

Map<String, Integer> someMap = new HashMap<>();

You can change the implementation later on(If you want) to point to a LinkedHashMap: -

someMap = new LinkedHashMap<>();

Whereas if you use HashMap on LHS, you can only make it point to an object of type HashMap..

But, as such there is no difference in performance.. But it is suggested to always design to an interface rather than implementation..

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
1

A interface defines, which method a class has to implement. This way - if you want to call a method defined by an interface - you don't need to know the exact class type of an object, you only need to know that it implements a specific interface.

Example:

interface Printer {
    public void print(String text);
}

class FilePrinter implements Printer {
    public void print(String text) {
       //append the text to a file
    }
}

class ScreenPrinter implements Printer {
    public void print(String text) {
       //write the text on the screen
    }
}

class SomeClass {
    public printSomething(Printer myPrinter) {
        myPrinter.print("Hello");
    }
}

If you call SomeClass.printSomething(...) it does not matter if you pass an instance of FilePrinter or ScreenPrinter, because the method just does not care. It knows that the object implements the interface Printer and also implements it's methods.

Another important point about interfaces is that a class can implement multiple interfaces.

Gigaquad
  • 153
  • 4