4

I am trying to understand what the benefits of using interfaces are so that I can know when and how to use them. Most sources on the internet are relatively surface-level, explaining how interfaces work but now why to use them, and when I look up the titular question, I don't get any results that tell me whether the purpose of interfaces extends beyond polymorphism and multiple inheritances.

My reasoning is that if an interface were inherited by only one class, it would be useless, and when an interface is inherited by multiple classes, it makes no difference unless it is used for polymorphism, and the only thing that makes implementation different from extension is multiple inheritances.

If I knew for sure that their purpose was limited to this, I would have an increased confidence in my design decisions, and if I learned of a purpose outside of this, it would fill a serious gap in my knowledge. I have used the design patterns tag because there is perhaps a design pattern which makes use of interfaces in a way that is distinctly beyond mere polymorphism or multiple inheritances.

  • 3
    The "other" side of *multiple inheritance* is a **contract**, which interfaces provide. For example, the `List` interface does not give any implementation worth inheriting by its implementing classes (making multiple inheritance quite a thin objective). The deal is that you'll have many classes behaving according to different properties, but abiding by the same "list" contract. This way, a design by contract can provide an API developer and an API caller/consumer a reliable contract, while at the same time allowing them to be loosely coupled. – ernest_k Feb 07 '22 at 06:37
  • @ernest_k Is there anything about interfaces (as opposed to anything else that could contain methods) that makes it easier to design by contract? I thought design by contract was mostly just about making sure encapsulation is followed to keep methods tamper-proof. – Stefan Lapointe Feb 07 '22 at 06:44
  • 1
    Strictly, not that much. Interfaces give a "type" kind of contract. Design by contract extend well beyond that (including method signatures, exceptions, and stated/assumed responsibilities of each party)... That's my understanding anyway. The fact is that interfaces give you the ability to have a contract that affords you polymorphism (in the sense of same type, different implementations) is a potent tool in design by contract, imo. Have you read this [programming to an interface question](https://stackoverflow.com/questions/383947/what-does-it-mean-to-program-to-an-interface)? – ernest_k Feb 07 '22 at 06:53
  • @ernest_k Thank you! I'm beginning to see the use of interfaces as a tool for more organized code that is easier to modify. Perhaps in a reductionist sense they are just about polymorphism but that view looks only at functionality would ignore design motivation. I like this idea of a contract for a whole type as opposed to an individual method. Organizing code according to that abstraction seems like a good motivation for creating an interface. The link was very helpful. – Stefan Lapointe Feb 07 '22 at 17:10

2 Answers2

2

Assuming that you're talking about the language feature (e.g. interface keyword in Java), as opposed to the general computing term, the purpose of interfaces is polymorphism.

A tool such as interfaces can be abused for other purposes, for example:

  • As a way of communicating commonality - this can backfire, because if polymorphism isn't the goal of the design, then the classes which declare implementing the interface are making an unnecessary commitment to implement it. That may cease to be relevant when the commonality is eventually broken, which can happen because the classes aren't used polymorphically.

  • As a way of documenting the contract and allowing the class implementation to change - In Java, this is achieved with public/protected methods with Javadoc are the way to document the contract. Some languages don't even have that, and they still document contracts. Of course, this only works if the contract comes in form of function calls (as opposed to e.g. RESTful HTTP APIs), and it only works if you have a rule about what you document, e.g. package boundary; you wouldn't want to create an interface for every class, even if you document the contract for every class.

  • To physically hide stuff from the consumer of your interface - this is also about documenting the contract, but if your class has data, or protected methods, and you want to prevent anything outside your package from inheriting, you can expose only an interface. But you can also use final.

Interfaces aren't designed to achieve multiple inheritance; they rather facilitate multiple inheritance only to the extent that is useful for polymorphism; it doesn't really allow you to inherit any fields, and until recently in Java with default methods (so, not by design), not even code.

You would see, in the wild, packages where only one class implements the interface. That doesn't render the interface useless; more implementations may come in the future, and in fact, the package might want to allow callers to offer their own implementation.

root
  • 5,528
  • 1
  • 7
  • 15
2

if an interface were inherited by only one class, it would be useless

I agree, but you can read a lot of discussion on this topic in Java Interfaces Methodology: Should every class implement an interface?

when an interface is inherited by multiple classes, it makes no difference unless it is used for polymorphism

I agree, it would be silly to implement an interface and then bypass it by invoking its methods directly on the concrete class. The purpose of implementing an interface is to have its abstract methods called, which is polymorphism in action. Since polymorphism is the primary tool of Object Oriented Programming, you could take this statement a step further and say that OOP makes no difference unless it is used for polymorphism.

the only thing that makes implementation different from extension is multiple inheritances

No, the primary difference between these Java constructs is that interface implementation facilitates stateless inheritance whereas class extension facilitates stateful inheritance. Java happens to support multiple stateless inheritance and single stateful inheritance. This dichotomy may be considered a mistake.

Do interfaces have any purpose besides achieving polymorphism and multiple inheritance?

In practice, yes. You can find interfaces used for other purposes such as constant interfaces or marker interfaces. Whether these practices are advisable has been debated.

jaco0646
  • 15,303
  • 7
  • 59
  • 83