Difference between interface and polymorphism

10,568

Polymorphism is the abstract concept of dealing with multiple types in a uniform manner, and interfaces are a way to implement that concept. Code that interacts with an interface can interact with any type that provides that interface.

Note that C++ has (at least) two forms of polymorphism: dynamic (i.e. run-time) polymorphism via interfaces formally defined by virtual functions, and static (i.e. compile-time) polymorphism via interfaces informally defined by the use of template parameters.

Share:
10,568
iart
Author by

iart

Updated on June 04, 2022

Comments

  • iart
    iart almost 2 years

    I was reading an online excerpt from a C++ book on polymorphism and interfaces. The book made a distinction between polymorphism and interfaces, and specified how to implement them in C++. However, I was always under the idea that interfaces in C++ (implemented using a base class with pure virtual functions) were nothing more than an application of polymorphism. I would like to know the clear distinction between polymorphism and interfaces because the excerpt confused me.

  • iart
    iart over 10 years
    The latter form of polymorphism relates to generic programming. I was concerned with the former. So interfaces are one of way of implementing polymorphism. Thank you for your answer! It really helped me
  • iart
    iart over 10 years
    Your explanation confirmed that one of the applications of polymorphism is an interface, but polymorphism is an abstract concept, while interfaces are used in code design? Thanks for the answer. I was able to wrap my head around this concept.
  • Mats Petersson
    Mats Petersson over 10 years
    Yes, like Juanchopanza says, Polymorphism is transport, Interface is a part of a car engine.