Are Interfaces just “Syntactic Sugar”?

2019-03-26 07:34发布

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 can implement the same interface, but multiple inheritance doesn't provide this as well?

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?

Do Interfaces were created in another languages because they don't provide multiple inheritance? Or am I missing something more important here?

9条回答
对你真心纯属浪费
2楼-- · 2019-03-26 08:25

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.

查看更多
唯我独甜
3楼-- · 2019-03-26 08:25

In dynamically typed languages, like PHP and Python, interfaces are only of limited use. You can already attempt to call methods on any object whenever, and you get a run-time error if it doesn't exist.

It's in statically typed languages, like Java and .NET, that interfaces become important, because methods and their arguments are checked at compile-time.

Now, for interfaces:

Java has Lists in addition to arrays. As a general rule, arrays are for primitives (the number types mainly), while Lists are for objects.

I can have a List<String>, which is a list of strings. I know I can add strings to it, and get strings back from it.

I don't know which implementation it is. It could be an ArrayList (list backed by an array), a LinkedList (list backed by a doubly linked list), a CopyOnWriteArrayList (thread-safe version of ArrayList), etc...

Thanks to polymorphism and interfaces, I don't need to know which type of List it is to do List operations on it.

查看更多
叛逆
4楼-- · 2019-03-26 08:36

It's generally implemented to replace multiple inheritance (C#). I think some languages/programmers use them as a way of enforcing requirements for object structure as well.

查看更多
登录 后发表回答