7

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
  • 286
  • 1
  • 3
  • 8
  • 2
    [go through this](http://stackoverflow.com/questions/10973949/difference-between-inheritance-and-polymorphism) – A B Jan 28 '14 at 07:42
  • This tells what interfaces and polymorphism are. However, I am trying to draw a distinction. Interfaces just seem to be an application of polymorphism from what I understand – iart Jan 28 '14 at 07:48
  • 3
    Not a good question, shows minimal effort. If this guy didn't bother to do a cursory google search, why the hell would he read my response. – Mikhail Jan 28 '14 at 07:48
  • 1
    It is like asking for the difference between a carburetor and transport. You can use a carburetor to aid some means of transport. But listing the differences is futile. – juanchopanza Jan 28 '14 at 07:49
  • 1
    I did do a google search, and was unable to understand the differences. I came to SO to ask this because I was unable to find the answer to my question and thought I might find people who might resolve my question. Sorry, if I offended you in any way – iart Jan 28 '14 at 07:50

3 Answers3

14

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.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • 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 Jan 28 '14 at 07:54
0

Typical C++ interfaces make use of virtual functions and polymorphism to provide the actual implementation. But polymorphism covers many other things, which is "not interfaces".

An interface is a base-class that can be used to a class that is passed back to something that accepts that interface. In some cases, a class may provide more than one interface:

 class MyClass: public InterfaceGUI, InterfaceAudio
 {
   .... 
 };

here, MyClass provides a class that interfaces both with the GUI and the Audio interfaces. This is a one case of multiple inheritance.

On the other hand:

 class Animal
 {
    int numLegs;
  public:
    Animal(int nLegs): numLegs(nLegs) {}
 };

 class Dog : public Animal
 {
    Dog() : Animal(4) { }
 };

here Animal is not a pure interface class, since it implements some functionality, and this is generally not a good design for an interface.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • 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. – iart Jan 28 '14 at 07:59
  • Yes, like Juanchopanza says, Polymorphism is transport, Interface is a part of a car engine. – Mats Petersson Jan 28 '14 at 08:06
0

What i understood from the concept of polymorphism and Interface is as follows:

  1. Polymorphism : Polymorphism is nothing but having more than one form(As per all books). But As per the Object orientation when you relate it with the real life You will get to know that Polymorphism is nothing but having more than one form with same name or some other Quality but have one's own Patent quality which no one have. As per the programming: A function with same name But different arguments (Compile time Polymorphism) and a Virtual function used for Runtime Polymorphism As explained here

Check this Example

  1. Interface

An interface describes the behavior or capabilities of a C++ class without committing to a particular implementation of that class.

The C++ interfaces are implemented using abstract classes and these abstract classes should not be confused with data abstraction which is a concept of keeping implementation details separate from associated data.

For this purpose we use Pure Virtual function For Reference you can go through this Link

And for example you can use explanation by Mats Petersson

Hope this will help you to understand the basic senario.

Community
  • 1
  • 1
A B
  • 1,461
  • 2
  • 19
  • 54