3
votes

(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 ?

1
nb I didn't find other answers which answer to that exact scenario/aspect. Also - this question comes from an argument with a colleague. - Royi Namir
I think you are right if we only stick to the three definitions given on Wikipedia. Some people may doubt Wikipedia is enough an "academic" source to base such an argument on. Do you have other references? - Fildor
Looks identical to the subtype polymorphism example to me, considering that the example was an abstract class, so T itself cannot be passed in anymore than an un-implemented interface can. - Magus
Your mistake is in taking the Wikipedia entry as prescriptive where it is only descriptive. Polymorphism is not a concept which can be easily subcategorized, and isn't even a well-defined concept in the first place. Rather, it is a blanket term for a family of related concepts, each particular language providing its own exact definition and formalization. - William F. Jameson
Most paradigms turn out ill-defined when you go pedantic about them. You might even say that's the nature of a paradigm, as opposed to a formalism. - William F. Jameson

1 Answers

1
votes

I think you are on the right track. Interfaces are not polymorphic themselves. They formalize polymorphism. They allow you to define polymorphic behavior in a declarative way, instead of implementation. I like to think of Interfaces as the bouncers in the club. They just make sure everyone is following the polymorphic rules.

In your example the actual polymorphic behavior isn't related to the interface itself but the method that is shared. The walk method fits the subtype polymorphism example. The interface itself just contractually obligates the child objects to walk.

UPDATE:

based on comment: just to make it clear - looking at an object via different interfaces it implements - is also polymorphic behaviour ?

The interface itself is just a contract (which you nailed in your question). The polymorphism comes from the methods on the contract. "A polymorphic type is a type whose operations can also be applied to values of some other type, or types" (from wikipedia). The interface (contract) is the agreement that holds the methods (terms) of service or use. So each contract hold the terms that are the polymorphic behavior.

Dog implements IWalk, ITalk, IAnimal
{
    //concrete implementation
}

IWalk 
{
    void walk();
}

ITalk
{
    String talk();
}

IAnimal
{

}

I am rusty on java so consider this pseudocode if its not syntactically correct.

IAnimal in this case is just a contract that does not contain polymorphic behavior. IWalk and ITalk promote polymorphism.