Welcome~~~


Another blog:
http://fun-st.blogspot.com/

It is easier to write an incorrect program than understand a correct one.

Wednesday, March 2, 2011

Interface Differences between Java and Python

# when is using an interface necessary?
==>when you want to mix up types that do not essentially fall in the super-subclass hierarchy, but they all follow certain contract (interface methods)
Java uses interfaces because it doesn't have multiple inheritance.

but python has multiple inheritance, when python is doing this, it is often intended:
class Abstract1( object ):
   """Some description that tells you it's abstract,
   often listing the methods you're expected to supply."""
   def aMethod( self ):
       raise NotImplementedError( "Should have implemented this" )
Python >= 2.6 has Abstract Base Classes.: Abstract Base Classes (abbreviated ABCs) complement duck-typing by providing a way to define interfaces when other techniques like hasattr() would be clumsy.
Python doesn't really have either concept.
It uses duck typing, which removed the need for interfaces (at least for the computer :-))
Python <= 2.5: Base classes obviously exist, but there is no explicit way to mark a method as 'pure virtual', so the class isn't really abstract.
Python >= 2.6: Abstract base classes do exist (http://docs.python.org/library/abc.html)

Below post talked about the difference between the implementation of interface in Python and Java:
http://dirtsimple.org/2004/12/python-interfaces-are-not-java.html

The interface vs. abstract in Java:
http://mindprod.com/jgloss/interfacevsabstract.html

--

♥ ¸¸.•*¨*•♫♪♪♫•*¨*•.¸¸♥

No comments:

Post a Comment