views:

382

answers:

9

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?

A: 

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.

Jay
+1  A: 

Because sometimes you don't want to provide an implementation.

Java Interface

Java class

Woot4Moo
+3  A: 

Because you want to program against an interface and not a concrete implementation (GoF 1995:18)

Gordon
hmm last I heard in the big consultant best practice book you code to abstract classes these days.
Woot4Moo
@Woot4Moo: In most languages, interfaces are classes that have all their methods abstract. This isn't quite true in .NET, though...
R. Bemrose
@Woot4Moo: it depends. Abstract classes are more flexible, but can force you to [deal with multiple inheritance](http://stackoverflow.com/questions/225929/what-is-the-exact-problem-with-multiple-inheritance/226056#226056). As always, use the right tool for the right job!
ladenedge
@ladenedge I wish more of my colleagues would think gloves instead of designing systems to keep the human body warm during winter bike rides.
Woot4Moo
+4  A: 

Even in duck-typed languages like Python, an interface can be a clearer statement of your intent. If you have a number of implementations, and they share a set of methods, an interface can be a good way to document the external behavior of those methods, give the concept a name, and make the concept concrete.

Without the explicit interface, there's an important concept in your system that has no physical representation. This doesn't mean you have to use interfaces, but interfaces provide that concreteness.

Ned Batchelder
+7  A: 

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.

Daniel Pryden
+1: *decoupling* is the key here.
ladenedge
-1 for "interfaces not adding significant value in PHP".
Lotus Notes
@Byron: I'm no expert on PHP, so enlighten me: what do you consider to be the *significant* value of interfaces in PHP (versus duck typing as is the Python idiom)? Specifically, are there any operations that are *impossible* to implement without them?
Daniel Pryden
@Daniel Pryden That seems to be a very contrived question. There's no reason to prove that there is something that cannot be implemented without them. I could just as easily ask if there are any operations in C that cannot be implemented somehow using Assembly. It's strange how you put down PHP (and PHP programmers) in some of your answers, yet you can't see that all of the OOP good practices are of equal value in a dynamically and/or weakly typed language. Just because one can technically call non-existent methods and wait for a run-time error doesn't mean one should. Use an interface.
Lotus Notes
@Byron: I'm not trying to "put down" PHP, or PHP programmers. I'm sorry if you get that impression. PHP, like any programming language, is a tool, and all tools have both strengths and weaknesses. To argue otherwise is simply absurd. All I was trying to say is that in a dynamically-typed language *like* PHP, interfaces don't add *significant* value. Since in a dynamic language, all errors occur at run-time anyway, regardless of whether or not interfaces are being used, the value-add of interfaces is *less* than in a language like Java. Surely we can agree on that?
Daniel Pryden
As I tried to explain in my answer, in a statically-typed language (especially a statically-typed language with early binding, like C++), interfaces (or an equivalent construct like pure virtual methods) are *essential* to be able to decouple implementations. It is simply not possible to decouple implementations without using something equivalent to interfaces or something at least as complex. Hence, having syntactic support for interfaces makes Java *more expressive* than C++ (although not having support for multiple inheritance makes Java *less expressive* than C++).
Daniel Pryden
Thank you for editing your answer (removed downvote). "Expressiveness" is a more appropriate term rather than the actual value and intent of interfaces, which should be the same whether or not the language is a compiled language. I personally couldn't imagine using PHP without some of the built-in interfaces like Serializable, Iterator, and RecursiveIterator.
Lotus Notes
+12  A: 

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.

S.Lott
+1 for the last 3 paragraphs.
Lotus Notes
+1  A: 

Yes. As for PHP, interfaces are just a means to overcome the lack of multiple inheritance. There are minor semantic differences useful for IDEs, and fewer conflicts caused by interfaces clearly aid newbie programmers. But as said before, it's not strictly necessary in dynamic languages. http://c2.com/cgi/wiki?MultipleInheritance

mario
+4  A: 

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.

R. Bemrose
+1  A: 

Please read Twisted Framework article about power of Zope Interfaces in python.

Surya