Under the hood of inheritance and polymorphism
Inheritance and polymorphism are two of the four main principles that OOP has. The four principles are as follows:
- Abstraction
- Encapsulation
- Inheritance
- Polymorphism
We have already talked about the first two principles, and now it is time to dive deeper into the final two – inheritance and polymorphism.
Inheritance
Classes can be reused, thanks to the programming notion of inheritance. Different programming languages offer various inheritance implementations, but the underlying principle is always the same – the class relationship should answer the is-a question. For instance, Car
is Vehicle
; hence, we can inherit Car
from Vehicle
:
class Vehicle {public: void move(); }; class Car : public Vehicle { public: Car(); // ... };
Car
now has the move()
member function derived from Vehicle
. The relationship between generalization and specialization...