(pedantic question)
According to wikipedia there are 3 types of polymorphism :
- Ad hoc polymorphism
refer to polymorphic functions which can be applied to arguments of different types, but which behave differently depending on the type of the argument to which they are applied
In other words : overloading :
function Add( x, y : Integer ) : Integer;
...
function Add( s, t : String ) : String;
- Parametric polymorphism
allows a function or a data type to be written generically, so that it can handle values identically without depending on their type
In other words : Generics
Example :
class List<T> {
class Node<T> { ...
- subtype polymorphism
allows a function to be written to take an object of a certain type T, but also work correctly if passed an object that belongs to a type S that is a subtype of T
(the most common usage)
Example :
abstract class Animal {
abstract String talk();
}
class Cat extends Animal {
String talk() {
return "Meow!";
}
}
...
Another Example :
class Animal
{
public virtual void eat()
{
Console.Write("Animal eating");
}
}
class Dog : Animal
{
public override void eat()
{
Console.Write("Dog eating");
}
}
Great .
Now I would like to show you the definition of interface :
Interfaces - An interface defines a contract that can be implemented by classes and structs. An interface can contain methods, properties, events, and indexers. An interface does not provide implementations of the members it defines—it merely specifies the members that must be supplied by classes or structs that implement the interface.
Great.
Question:
Looking at this pseudo code :
Dog implements IWalk {...}
Cat implements IWalk {...}
Array[IWalk] = [new Dog(),new Cat()]
foreach item in array : item.walk();
- Is this polymorphism behaviour (invoking walk() on each different object)?
IMHO it is not. why ? because it doesn't corresponds to any of wiki categories mentioned above.
I believe it is pure principle of coding where I look at an object via different glasses - NOT creating/mutating functionality as the 3 paradigms above mention
Am I right ? is this polymorphic behaviour or not ?