4

In object oriented programming, I have read that you should program to an interface not an implementation but do they mean literal interfaces (no shared code at all)?

Is it okay to program to an abstract base class that would have been an interface except that there were variables in this "interface" that all sub-classes were expected to have? Replicating a variable across sub-classes would have been an inconvenience because if I changed the name of one of the variables in one of the sub-classes I would have to change the name of that variable in all of the sub-classes.

In following the principle of "program to an interface not an implementation", is this okay or would you create another interface on top of the abstract base class and program to that interface?

Anthony Forloney
  • 90,123
  • 14
  • 117
  • 115

2 Answers2

2

Note: C++ doesn't have interfaces. You might argue it doesn't need them.

you should program to an interface not an implementation but do they mean literal interfaces (no shared code at all)?

Possibly. Where it makes sense to do this, it can work very well. Note: in Java interfaces can have code as well.

Is it okay to program to an abstract base class that would have been an interface except that there were variables in this "interface" that all sub-classes were expected to have?

If you need fields in the implementation an abstract class can make sense. You can still use an interface as well.

Replicating a variable across sub-classes would have been an inconvenience because if I changed the name of one of the variables in one of the sub-classes I would have to change the name of that variable in all of the sub-classes.

This is where using an IDE helps. You can change a field, class, method name in all your code with one action.

is this okay or would you create another interface on top of the abstract base class and program to that interface?

You can code your implementation to an abstract class, but the users of that class should be using an interface if possible.

e.g. HashMap extends AbstractMap but implements Map. Most people would use Map not AbstractMap

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
2

You want to program to interfaces because it means lower coupling. Note that interfaces in Java are more flexible since they can be implemented by a class anywhere in the class hierarchy unlike abstract classes (as a result of single inheritance). Such flexibility means that your code is reusable to a higher degree.

The important point of "programming to an interface not an implementation" is that of the general principles mentioned above, even if they might cause some minor inconveniences.

Also, even if you program to an interface, you can always implement said interface (or parts of it) by means of abstract classes if you'd like, achieving both low coupling and code reusability at the same time.

It's always okay to program to abstract or even concrete classes, however it's better if you can avoid it.

This discussion might be helpful or this one and of course this one.

Community
  • 1
  • 1
olovb
  • 2,164
  • 1
  • 17
  • 20
  • 2
    I vote your answer most helpful because of the line "It's always okay to program to abstract or even concrete classes, however it's better if you can avoid it." –  May 06 '15 at 22:23