10

Im learning about oop just now and am struggling with the difference between inheritance and polymorphism.

If i understand correctly, inheritance defines a type hierarchy and the relationship between classes. Child classes inherit behaviour from the parent class and can provide specialised behaviour of any public interface on the parent.

Polymorphism is that these child classes can provide their own behaviour while still adhering to the parent interface.

This is the same thing isnt it?

Alex
  • 21,273
  • 10
  • 61
  • 73
Marty Wallace
  • 34,046
  • 53
  • 137
  • 200
  • The polymorphism provided by subtypes is only one kind of polymorphism; see [Wikipedia](http://en.wikipedia.org/wiki/Polymorphism_(computer_science)) for details, since I don't have time to write up a whole answer. – user2357112 Jun 11 '14 at 21:32
  • Related: [Can inheritance be replaced completely by composition?](https://stackoverflow.com/a/53108601/1371329) – jaco0646 Nov 13 '18 at 17:46

3 Answers3

11

You are correct that in most OO languages, inheritance and polymorphism go one with another. But:

  1. Polymorphism and Inheritance both illustrate different aspects. Polymorhpism shows how the types can stand one for another in operations, while the inheritance shows the relationship between the types.
  2. Polymorphism and Inheritance don't have to go together in general. If you look at integer and float numbers for example, there all of them can stand at the same places of arithmetic expression, so it is in fact polymorphism, but there is no inheritance. Also, inheritance is possible without polymorphism.
Tomas
  • 57,621
  • 49
  • 238
  • 373
2

The best way to look at it is that polymorphism is possible thanks to inheritance. Inheritance defines the hierarchy and the is-a principle, polymorphism can be achieved because of this.

  • Many languages have polymorphism without inheritance, especially dynamic languages that implement duck typing. – jaco0646 Nov 13 '18 at 17:36
0

Inheritance could also be used (not considered a good practice anymore) to inherit and extend functionality.

class MovableObject {
  protected: // usable in subclasses
    position: Point;

  public:
    void Move(Vector diff) { position += diff; }
}

class Car: private MovableObject { // private inheritance hides the interface
  private:
    float fuelLeft;

  public Drive(Vector diff, float fuelSpent) {
    Move(diff);
    fuelLeft -= fuelSpent;
  }
}

Of course, this is not the best class design and it isn't meant to be, but it illustrates how you can have inheritance without polymorphism.

Grozz
  • 8,317
  • 4
  • 38
  • 53
  • what is not a good practice about this? This is quite a standard use of inheritance, isn't it? – Tomas Jun 11 '14 at 21:40
  • Diamond-problem and restrictions this approach puts on your code in languages (most of them nowadays) with single class inheritance. – Grozz Jun 11 '14 at 21:45
  • So, what is the actual good practice to write the code you sketched above? – Tomas Jun 11 '14 at 21:52