Polymorphism and interfaces - clarification?

(pedantic question)

According to wikipedia, there are 3 types of polymorphism:

  • Special polymorphism

refer to polymorphic functions that 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: overload:

function Add( x, y : Integer ) : Integer;
...
function Add( s, t : String ) : String;

      

  • Parametric polymorphism

allows you to write a function or data type in a generic way, so that it can treat values ​​the same, regardless of their type

In other words: Generics

Example:

class List<T> {
    class Node<T> { ...

      

  • subtype polymorphism

allows you to write a function to take an object of a specific type T, but also work correctly if passed an object belonging to type S which is a subtype of T

(most common use)

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");
    }
}

      

Fine.

Now I would like to show you the definition of an interface:

Interfaces . An interface defines a contract that can be implemented through classes and structures. An interface can contain methods, properties, events, and indexes. An interface does not provide the implementations of the members it defines - it simply specifies the members that should be provided by the classes or structs that implement the interface.

Great.

Question:

Looking at this pseudocode:

Dog implements IWalk {...}
Cat implements IWalk {...}
Array[IWalk] = [new Dog(),new Cat()]
foreach item in array :  item.walk();

      

  • Is this behavior of polymorphism (calling walk () on every other object)?

IMHO it is not. What for? because it doesn't fit any of the wiki categories mentioned above.

I believe this is a pure coding principle where I look at an object through different glasses - NOT creating / mutating functionality as 3 paradigms above mentioned

I'm right? is this polymorphic behavior or not?

+3


source to share


1 answer


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

In your example, the actual polymorphic behavior is not related to the interface itself, but to the shared method. The walk method is suitable for an example of subtype polymorphism. The interface itself just obliges the child objects to walk.

UPDATE:

based on a comment: just to figure it out - looking at an object using the various interfaces it implements - also polymorphic behavior?



The interface itself is just a contract (which you nail in your question). Polymorphism comes from contract methods. "A polymorphic type is a type whose operations can also be applied to values ​​of some other type or types" (from wikipedia). An interface (contract) is an agreement that contains methods (terms) of service or use. Thus, each contract contains terms that are polymorphic behavior.

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

IWalk 
{
    void walk();
}

ITalk
{
    String talk();
}

IAnimal
{

}

      

I'm rusty on java so consider this pseudocode if not syntactically correct.

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

+1


source







All Articles