Inheritance

Inheritance

Inheritance is the mechanism by which one class can acquire the attributes and methods of another, extending or specializing them. It is a primary tool for code reuse and for expressing hierarchical relationships between kinds of things. When a class inherits from another, the class being inherited from is the superclass (also called parent or base class) and the class doing the inheriting is the subclass (child or derived class).

The "is-a" relationship

Inheritance models an "is-a" relationship. A Dog is-a Animal; a SavingsAccount is-a BankAccount; a Circle is-a Shape. Whenever you can sensibly say "X is a kind of Y," inheritance from Y may be appropriate. The subclass automatically gains everything the superclass declared as accessible, and then adds its own extra attributes and methods,