Part 1
- While object compositions can model a “has-a” relation, inheritance allows us to model an “is-a” relationship between two objects. Attributes and behaviours of other objects are inherited and can then be extended, i.e. they are similar to existing objects, but with special functionality and/or data.
- Examples of objects using inheritance in the real world:
Base class: Vehicle; Inherited classes: Car, Bicycle, Moped
Base class: InputDevice; Inherited classes: Mouse, Keyboard, Joystick
Part 2
- A superclass, also called base class or parent class, is the class being inherited from, whereas a subclass, also called derived class or child class is the class doing the inheriting.
class Subclass : public Superclass { ... }
- A subclass can extend the functionality of a superclass by adding both new methods and new member variables.