Mahammad Adil Azeem
Mahammad Adil Azeem

Reputation: 9392

Difference between Java Interfaces and Python Mixin?

I have been reading about Python-Mixin and come to know that it adds some features (methods) to class. Similarly, Java-Interfaces also provide methods to class.

Only difference, I could see is that Java-interfaces are abstract methods and Python-Mixin carry implementation.

Any other differences ?

Upvotes: 2

Views: 1025

Answers (1)

jbrendel
jbrendel

Reputation: 2448

Well, the 'abstract methods' part is quite important.

Java is strongly typed. By specifying the interfaces in the type definition, you use them to construct the signature of the new type. After the type definition, you have promised that this new type (or some sub-class) will eventually implement all the functions that were defined in the various interfaces you specified.

Therefore, an interface DOES NOT really add any methods to a class, since it doesn't provide a method implementation. It just adds to the signature/promise of the class.

Python, however, is not strongly typed. The 'signature' of the type doesn't really matter, since it simply checks at run time whether the method you wish to call is actually present.

Therefore, in Python the mixin is indeed about adding methods and functionality to a class. It is not at all concerned with the type signature.

In summary:

  • Java Interfaces -> Functions are NOT added, signature IS extended.
  • Python mixins -> Functions ARE added, signature doesn't matter.

Upvotes: 11

Related Questions