I am struggling with applying the "programming to an interface" guideline because I can't seem to decide in which situations it is necessary and in which ones it's overkill (or even counter productive).

I would like to know of any heuristics or rules that you use in your projects to decide if you need to create a new interface and use it instead of an specific class.

Just to clarify, note that the question is not about the benefits of this guideline or the importance of following it. It's about some basic rules you use to know when to use it and when not to.

  • 3
    see also: Understanding “programming to an interface”
    – gnat
    Jun 26 '16 at 15:12
  • @gnat thanks for the references, but please see that I am not asking for an explanation of the benefits or to know if it has to be applied in one particular case. What I need to know is if you have specific rules/heuristics you follow on a daily basis to decide when and when not to follow this guideline. (Btw, i read those questions and some others before posting, and I don't think they answer my question). Jun 26 '16 at 15:35
  • 4
    "Program to an interface" doesn't mean "program to the Java interface keyword." It means, essentially, "Respect the API contract." When do you do that? Always. Read Telastyn's answer carefully. Jun 26 '16 at 16:19
  • 1
    I'm just concerned that you're confusing the interface that the "programming to an interface" guideline describes with the interface that is provided in some programming languages. See fatagnus.com/program-to-an-interface-not-an-implementation. Jun 27 '16 at 0:12
  • 2
    @carlossierra Generally, an interface means the surface of something. In programming, it's whatever a component provides for consumption and manipulation by other components, like those you might write. Such an interface consists of everything the provider says belongs to it, no more, no less: Names (or absence thereof), constants, functions, classes, whatever, and their behavior. Beware though that existence of something does not make it part of the interface. Jun 27 '16 at 0:40