Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Isn't polymorphism just a side effect of inheritance?

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?

like image 441
Marty Wallace Avatar asked Jun 11 '14 21:06

Marty Wallace


1 Answers

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.
like image 118
Tomas Avatar answered Sep 28 '22 23:09

Tomas