3

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

Royi Namir
  • 144,742
  • 138
  • 468
  • 792
  • 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 Aug 06 '14 at 14:59
  • 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 Aug 06 '14 at 15:04
  • 1
    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 Aug 06 '14 at 15:05
  • In this case you are simply using the concept of an interface that doesn't necessarily has to use the concept of polymorphism. You could of course define an interface that involves it: `interface IWalk{void Walk(); void Walk(bool fastWalk)}` – T_D Aug 06 '14 at 15:07
  • @Magus whata bout the seond sample of code ? `Animal a = new Animal(); a.eat()` would yield differnt result than `Animal a = new Dog(); a.eat();` this is the power of poly. – Royi Namir Aug 06 '14 at 15:21
  • 2
    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 Aug 06 '14 at 15:26
  • @WilliamF.Jameson interesting comment . I thought it's a well defined paradigm – Royi Namir Aug 06 '14 at 15:32
  • 1
    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 Aug 06 '14 at 15:40
  • @RoyiNamir: Yes. Exactly like an interface. – Magus Aug 06 '14 at 15:50
  • I think another logical error here is comparing the concept of something to a particular implementation of the same thing. For instance, C++ didn't have interfaces, but did polymorphism through abstract base classes and pointers to virtual functions. Java does it a bit differently and introduced interfaces. No pointers involved, but pretty much the same. In C#, you can do "true" polymorphism with interfaces because you can do explicit implementations, so it really does matter what type of interface you called it through. – Jeff Walker Aug 06 '14 at 18:20

1 Answers1

1

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.

Robert
  • 4,306
  • 11
  • 45
  • 95
  • my point is that if I look at an object via interface , the object wont change its implementation ever ! - all the methods are already there ! the interface just - like you said - glasses. whereas : `Animal a = new Dog(); a.eat()` will cause REAL polymorphic behaviour where implementation has changed !( at runtime). do you see my point ? – Royi Namir Aug 06 '14 at 15:27
  • The implementation hasn't changed, especially not at runtime. `Animal` is just a *type annotation* on the reference variable `a`, and `new Dog()` is an actual object. `a` on its own has no implementation. – William F. Jameson Aug 06 '14 at 15:28
  • @WilliamF.Jameson in `Animal a = new Dog(); a.eat()` - the method to invoke is chosen at runtime. it could be animal's or dog's. please look at my code above) - yes the reference type is animal. – Royi Namir Aug 06 '14 at 15:29
  • Choosing a method at runtime is not the same as "implementation changing at runtime". – William F. Jameson Aug 06 '14 at 15:30
  • @RoyiNamir Your implementation remains the same on either account. Even though the decision is made at runtime it is still on one instance of that object not on the overall behavior of the implementation. The implementation itself is polymorphic and contractual. The instance itself is referenced and acted upon – Robert Aug 06 '14 at 15:31
  • @WilliamF.Jameson Yes. I take my words back. – Royi Namir Aug 06 '14 at 15:31
  • @Robert So — just to make it clear - looking at an object via different interfaces it implements - is also polymorphic behaviour ? – Royi Namir Aug 06 '14 at 15:38
  • @Robert, so you say that the fact that interface methods have different implementations make that interface function polymorphic? And if so, then if a subclass overrides the parent’s implementation of a method then it again becomes polymorphic? I still do not get the picture, because most people insist that polymorphism is strictly a type-theory stuff meaning a function can accept more types per parameter when these types have something in common (for example, a common superclass/interface being the common property which is subtype polymorphism). – Patrik Nusszer Jun 20 '19 at 10:42
  • @PatrikNusszer Check out this post and see if that helps you any: https://stackoverflow.com/questions/1031273/what-is-polymorphism-what-is-it-for-and-how-is-it-used – Robert Jun 26 '19 at 18:30