Reputation: 89
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 not 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.
Upvotes: 4
Views: 1183
Reputation: 17066
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.
Upvotes: 2
Reputation: 6058
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.
Upvotes: 2