Why do dynamic languages like Ruby and Python not have the concept of interfaces like in Java or C#?
Asked Answered
A

5

20

To my surprise as I am developing more interest towards dynamic languages like Ruby and Python. The claim is that they are 100% object oriented but as I read on several basic concepts like interfaces, method overloading, operator overloading are missing. Is it somehow in-built under the cover or do these languages just not need it? If the latter is true are, they 100% object oriented?

EDIT: Based on some answers I see that overloading is available in both Python and Ruby, is it the case in Ruby 1.8.6 and Python 2.5.2 ??

Ainsworth answered 3/4, 2009 at 20:26 Comment(4)
I don't recall method overloading or operator overloading being a requirement for an OO language. However, I have heard the argument that they're not OO.Scud
Of all the languages you mention, Java is the only one without operator overloading. And method overloading isn't related to OOP; C has method overloading.Crux
You can overload operators in Ruby 1,8,6Ity
PHP does have interface.Kilian
C
18

Thanks to late binding, they do not need it. In Java/C#, interfaces are used to declare that some class has certain methods and it is checked during compile time; in Python, whether a method exists is checked during runtime.

Method overloading in Python does work:

>>> class A:
...  def foo(self):
...    return "A"
...
>>> class B(A):
...  def foo(self):
...    return "B"
...
>>> B().foo()
'B'

Are they object-oriented? I'd say yes. It's more of an approach thing rather than if any concrete language has feature X or feature Y.

Corelli answered 3/4, 2009 at 20:29 Comment(1)
Sorry that is not method overloading. What your doing there is method overriding. Method/function overloading is the ability to reuse the same name but with a different signature as in: func a(String a); func a(Int a);. In your example you use a method with the same signature, but overriden in a subclass. Wikipedia - Overloading and Wikipedia - OverridingAnking
G
21

Dynamic languages use duck typing. Any code can call methods on any object that support those methods, so the concept of interfaces is extraneous. Python does in fact support operator overloading(check - 3.3. Special method names) , as does Ruby.

Anyway, you seem to be focusing on aspects that are not essential to object oriented programming. The main focus is on concepts like encapsulation, inheritance, and polymorphism, which are 100% supported in Python and Ruby.

Glucose answered 3/4, 2009 at 20:30 Comment(0)
C
18

Thanks to late binding, they do not need it. In Java/C#, interfaces are used to declare that some class has certain methods and it is checked during compile time; in Python, whether a method exists is checked during runtime.

Method overloading in Python does work:

>>> class A:
...  def foo(self):
...    return "A"
...
>>> class B(A):
...  def foo(self):
...    return "B"
...
>>> B().foo()
'B'

Are they object-oriented? I'd say yes. It's more of an approach thing rather than if any concrete language has feature X or feature Y.

Corelli answered 3/4, 2009 at 20:29 Comment(1)
Sorry that is not method overloading. What your doing there is method overriding. Method/function overloading is the ability to reuse the same name but with a different signature as in: func a(String a); func a(Int a);. In your example you use a method with the same signature, but overriden in a subclass. Wikipedia - Overloading and Wikipedia - OverridingAnking
I
7

I can only speak for python, but there have been proposals for interfaces as well as home-written interface examples in the past.

However, the way python works with objects dynamically tends to reduce the need for (and the benefit of) interfaces to some extent.

With a dynamic language, your type binding happens at runtime - interfaces are mostly used for compile time constraints on objects - if this happens at runtime, it eliminates some of the need for interfaces.

Ildaile answered 3/4, 2009 at 20:31 Comment(0)
B
2

name based polymorphism

"For those of you unfamiliar with Python, here's a quick intro to name-based polymorphism. Python objects have an internal dictionary that contains a string for every attribute and method. When you access an attribute or method in Python code, Python simply looks up the string in the dict. Therefore, if what you want is a class that works like a file, you don't need to inherit from file, you just create a class that has the file methods that are needed.

Python also defines a bunch of special methods that get called by the appropriate syntax. For example, a+b is equivalent to a.add(b). There are a few places in Python's internals where it directly manipulates built-in objects, but name-based polymorphism works as you expect about 98% of the time. "

Bichromate answered 3/4, 2009 at 20:34 Comment(0)
B
1

Python does provide operator overloading, e.g. you can define a method __add__ if you want to overload +.

You typically don't need to provide method overloading, since you can pass arbitrary parameters into a single method. In many cases, that single method can have a single body that works for all kinds of objects in the same way. If you want to have different code for different parameter types, you can inspect the type, or double-dispatch.

Interfaces are mostly unnecessary because of duck typing, as rossfabricant points out. A few remaining cases are covered in Python by ABCs (abstract base classes) or Zope interfaces.

Batha answered 3/4, 2009 at 20:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.