Inheritance
A class inherits state and behavior from its superclass.
Inheritance provides a powerful and natural mechanism for organizing
and structuring software programs.
Examples:
 
Each subclass inherits state
(in the form of variable declarations) from the superclass. Mountain
bikes, racing bikes, and tandems share some states: cadence, speed,
and the like. Also, each subclass inherits methods from
the superclass. Mountain bikes, racing bikes, and tandems share some
behaviors: braking and changing pedaling speed, for example.
- Subclasses can add their own variables and methods.
- Subclasses can also override inherited methods
and provide specialized implementations for those methods.
For example, if you had a mountain bike with an extra
set of gears, you would override the "change gears" method so
that the rider could use those new gears.
- The inheritance tree, or class hierarchy, can be as deep as
needed. Methods and variables are inherited down through the levels.
In C++: You
can have multiple parents
In Java: You can have
only 1 parent |
|