Are Interfaces just “Syntactic Sugar”?

前端 未结 2 1989
南笙
南笙 2021-02-15 06:22

I\'ve been playing mostly with PHP and Python.

I\'ve been reading about Interfaces in OO programming and can\'t see an advantage in using it.

Multiple objects ca

相关标签:
2条回答
  • 2021-02-15 06:29

    The usefulness of an interface is directly connected to the usefulness of static typing. If you're working in a dynamically-typed language like PHP or Python, interfaces truly don't add significantly to the expressiveness of the language. That is, any program that can be described as using interfaces can be expressed without significant difference without using interfaces.

    As a result, Python has a fairly nebulous concept of a "protocol" (an implementation conforming to a certain pattern, like the iteration protocol) which amounts to essentially the same thing, but without the other benefits of compile-time checking its value is limited.

    In a statically-typed language, on the other hand, an interface is essential to allow implementation to be decoupled from implementation. In a static language, the types of all expressions must be resolved at compile time, so normally bindings to implementation must be made at that time, limiting run-time flexibility. An interface defines how to access functionality without defining a specific implementation, which allows a static language to prove that expressions are correct without having access to the implementation.

    Without interfaces (or an equivalent formulation like C++'s pure virtual functions), the expressiveness of a statically-typed language would be severely hampered. In fact, many implementations exist (Win32 and COM come immediately to mind) to essentially reproduce much of the functionality of interfaces and virtual dispatch in C by storing function pointers in structures (and thus re-implementing C++'s virtual functions and vtable invocation by hand). In this case there is a big difference in expressiveness, since many changes are required in the program to express the same concepts.

    Interfaces are just one example of type polymorphism, and a fairly limited one at that. In languages that support parametric polymorphism (aka generics) you can accomplish much more. (For example, C#'s LINQ would not be possible without generic interfaces.) For a much more powerful form of the same kind of thing, look into Haskell's typeclasses.

    0 讨论(0)
  • 2021-02-15 06:38

    First, and foremost, try not to compare and contrast between Python and Java. They are different languages, with different semantics. Compare and contrast will only lead to confusing questions like this where you're trying to compare something Python doesn't use with something Java requires.

    It's a lot like comparing the number 7 and the color green. They're both nouns. Beyond that, you're going to have trouble comparing the two.

    Here's the bottom line.

    Python does not need interfaces.

    Java requires them.

    Multiple objects can implement the same interface, but multiple inheritance doesn't provide this as well?

    The two concepts have almost nothing to do with each other.

    I can define a large number of classes which share a common interface. In Python, because of "duck typing", I don't have to carefully be sure they all have a common superclass.

    An interface is a declaration of "intent" for disjoint class hierarchies. It provides a common specification (that can be checked by the compiler) that is not part of the simple class hierarchy. It allows multiple class hierarchies to implement some common features and be polymorphic with respect to those features.

    In Python you can use multiple inheritance with our without interfaces. Multiple inheritance can include interface classes or not include interface classes.

    Java doesn't even have multiple inheritance. Instead it uses a completely different technique called "mixins".

    Why do I need to create an Interface "with no implementation" - mainly a "contract" - if I can just check if a method exists in an object in Python, that inherits from multiple classes?

    If you create an interface in Python, it can be a kind of formal contract. A claim that all subclasses will absolutely do what the interface claims.

    Of course, a numbskull is perfectly free to lie. They can inherit from an interface and mis-implement everything. Nothing prevents bad behavior from sociopaths.

    You create an interface in Java to allow multiple classes of objects to have a common behavior. Since you don't tell the compiler much in Python, the concept doesn't even apply.

    Do Interfaces were created in another languages because they don't provide multiple inheritance?

    Since the concepts aren't related, it's hard to answer this.

    In Java, they do use "mixin" instead of multiple inheritance. The "interface" allows some mixing-in of additional functionality. That's one use for an interface.

    Another use of an Interface to separate "is" from "does". The class hierarchy defines what an objects IS. The interface hierarchy defines what a class DOES.

    In most cases, IS and DOES are isomorphic, so there's no distinction.

    In some cases, what an object IS and what an object DOES are different.

    0 讨论(0)
提交回复
热议问题