1

I see in Wikipedia and other text giving examples of polymorphism in this way:

1) If there is an array of 4 Animal objects, and they really are of Bird, Dog, Cat, Fish classes, then looping through them, and calling animal.move(), (where animal is a generic animal pointer), then that will be polymorphism.

2) If I have a Dog object, but I pass it to a plain function moveIt(Animal *animal) and inside, it uses animal.move() to invoke move(), and we can pass in a Fish object too (by moveIt(fish)), and it will do the right thing too, and that's polymorphism.

I think that's fine, the examples showed a generic animal object being able to invoke the appropriate method (wisely) as a dog or fish, and that's polymorphism.

But what about in languages such as JavaScript or Ruby, where array elements can be any type at all. So it is not an array of 4 animal objects, but an array of [bird, dog, cat, fish], and now we loop through the array and invoke it:

# In Ruby:

bird = Bird.new()
dog = Dog.new()
cat = Cat.new()
fish = Fish.new()

arr = [bird, dog, cat, fish]
arr.each do |i|
  i.move()
end

now in this case, since they never became a "generic" animal, and of course we expect dog.move() to behave as a dog's way of movement, so in this case, strictly speaking, will it still be polymorphism?

nonopolarity
  • 146,324
  • 131
  • 460
  • 740

2 Answers2

1

Yes, it's runtime polymorphism,i.e. not based on inheritance. Technically, it's called duck-typing, eg if it walks like a duck, talks like a duck then ...

The objects all have the move() method so the language allows for dynamic invocation by method signature alone.

https://en.m.wikipedia.org/wiki/Duck_typing

Inheritance based polymorphism which many people are familiar with is when a subclass overrides the behavior of one or more methods of one of the superclasses. When the method is invoked the specific subclass implementation is exercised based upon the objects class.

John Scattergood
  • 1,032
  • 8
  • 15
  • so runtime polymorphism is still polymorphism? what about the one based on inheritance, then is it call... relative polymorphism? and as I said, what if these 4 classes actually do inherit from `Animal` which implements a `move` method? – nonopolarity Dec 16 '15 at 03:01
  • Technically both are examples of polymorphism. If you are asking about how it's implemented then that is language specific. – John Scattergood Dec 16 '15 at 03:08
  • Ruby searches for a matching method at runtime even if the objects descend from the same ancestor: https://blog.jcoglan.com/2013/05/08/how-ruby-method-dispatch-works/ – John Scattergood Dec 16 '15 at 03:16
0

No, because they don't all inherit from the same base class.

The term you would want to use to describe this would be duck typing. The reason why the code example you give works is only because it so happens (likely by your design) that every object in the array has the appropriate method.

mr2ert
  • 5,146
  • 1
  • 21
  • 32
  • wait, I never said they don't inherit from a base class `Animal` -- they could, in their class definitions, and besides, even if they don't, they all still inherit from `Object`, don't they... or will you say `Object` doesn't count? – nonopolarity Dec 16 '15 at 02:34
  • @太極者無極而生 The `Object` type does not define a `move` method though. – mr2ert Dec 16 '15 at 02:43
  • that's true... then, what if these classes all inherit from `Animal`, which implements a `move` method, or just declare it as an abstract method? – nonopolarity Dec 16 '15 at 02:48