-4

Ok, I understand the basic concept of the interface, but I don't necessarily know that I "get" it's use.

I'm in the middle of writing an app at home right now using the Abstract Factory design pattern. For practice, mostly, but my ultimate goal is to sell the app (but that's not for this site).

Anyway, the book that I'm using as a reference uses an interface in the example.

Wikipedia explains that "Interfaces are used to encode similarities which the classes of various types share, but do not necessarily constitute a class relationship."

So that basically tells me that interfaces are used to ensure similarities between classes.

So if I have an interface called Guitar, I can have an abstract class called GuitarAbstract, and other classes called BassGuitar, AcousticGuitar, ElectricGuitar, with all of the inheritance and implementation, etc.

I understand, intellectually, the need and usage for interfaces. It just seems like additional, redundant code.

Am I missing something there?

MJR
  • 169
  • 2
  • 15
  • 1
    possible duplicate of [What does it mean to "program to an interface"?](http://stackoverflow.com/questions/383947/what-does-it-mean-to-program-to-an-interface) – Brian Roach Jul 11 '13 at 16:24
  • possible duplicate of [Interfaces in Java - what are they for?](http://stackoverflow.com/questions/14033992/interfaces-in-java-what-are-they-for) – Matt Ball Jul 11 '13 at 16:24
  • 3
    For the record, I think "too broad" is absolutely the wrong close reason. – Matt Ball Jul 11 '13 at 16:25
  • In languages that don't support multiple inheritance, such as Java, interfaces can be used to fill in the gap. – Paul Renton Jul 11 '13 at 16:25
  • They're a compromise. C++ and other OO languages had multiple inheritance, which was powerful but caused problems. An interface allows some aspects of multiple inheritance without many of the pitfalls. – MatBailie Jul 11 '13 at 16:25
  • @MJR look at all of the different Java `Map` and `Collection` implementations as an example. – Matt Ball Jul 11 '13 at 16:25
  • 3
    Duplicate question is more correct close reason. – William Morrison Jul 11 '13 at 16:25
  • it seems you haven't understand interfaces yet. –  Jul 11 '13 at 17:57

2 Answers2

3

An interface allows you to write code which will work with any type of class, as long as it implements the required interface. This allows your code to be easily reused reducing work. Interfaces are not redundant.

An Iterator is a good example. You don't care what datastructure the iterator represents, you just want to retrieve data.

This is the purpose of an interface.

William Morrison
  • 10,953
  • 2
  • 31
  • 48
1

An interface is not additonal code, it is like a contract. It defines, what functionality must be provided, and leaves the implementation details to the class. this is IMO the core of real OO prorgamming.

Devolus
  • 21,661
  • 13
  • 66
  • 113
  • That implies there is no need for class inheritance, you could do everything with interfaces. Which is plainly insane. The whole truth needs to reflect why these methods are abstract, but in class inheritance they're not. – MatBailie Jul 11 '13 at 16:27
  • class inheritance overrides existing implementation behaviour. In many cases you might be able to compleltey override it. In such a case, an interface is better, because you don't need to have a base implementation. And you don't have unneccessary dependencies with an interface. – Devolus Jul 11 '13 at 16:34
  • And you think it has nothing at all to do with a compromise to avoid the problems associated with multiple inheritance, while maintaining many of the benefits? – MatBailie Jul 11 '13 at 16:37
  • @MatBailie: It likely has 10% to do with your point and 90% to do with what Devolus is suggesting. – Hovercraft Full Of Eels Jul 11 '13 at 16:39
  • In the last ten years, I never once felt the need to use multiple inheritance, but I would have a huge problem of making real OO code if there wouldn't be the concept of interfaces. :) In fact, I always felt that multiple inheritance can cause problems which are goog reasons to not use it. – Devolus Jul 11 '13 at 16:39
  • 1
    Consider just inheritance. You have a base class which implements a defined set reading data from files. Now you want to retrieve the same data via a socket. You can override the base class and probalby 90% of the "base" class are usuless, because you must implemented everything else from scratch. But you still have the dependency on all that code involved, even though you never need it. With an interface you just implement the new class without any overhead. Use inheritance where it makes sense and interfaces where they make sense. They have different uses, even though they overlap seemingly. – Devolus Jul 11 '13 at 16:44