Search code examples
javainheritancepolymorphismterminologydynamic-binding

Is all dynamic binding a kind of polymorphism?


Is all dynamic binding considered to be polymorphism? Specifically, I'm talking about Java. If not, please explain both terms.

What I know: not all inheritance is polymorphism but inheritance is used in all polymorphism.


Solution

  • First of all dynamic binding (or late binding) is an improper term. You are talking about dynamic dispatch which is a different thing.

    Dynamic binding is choosing which implementation of a method will be called at runtime, but this doesn't happen in Java. But you have dynamic dispatch which is the ability to choose the correct polymorphic implementation of a method at compile time (which usually translates to choosing the most specialized version of the method/function).

    But I wouldn't say that dynamic dispatch is polymorphism, I'd say that to support polymorphism you need a mechanism to choose the correct implementation of a method and this is dynamic dispatch.

    Inheritance is a kind of polymorphism called subtyping (or subtype polymorphism) so, contrary to what you say, inheritance is always a form of polymorphism, but other kinds of polymorphism exist, think about:

    • ad hoc polymorphism (implemented through overloading) where the same function has different meanings when applied to different arguments
    • parametric polymorphism (implemented with Generics in Java) where you have types with type variables so that a single type can express infinite number of types by binding the variable to a specific type (or a class of types), think about List<Integer>