Java: Object-Oriented Design - Inheritance
Inheritance is one of the fundamental OOP technologies. Each class can be defined to inherit the data and methods from another class. This idea is extremely powerful and can lead to enormously productivity as it becomes easy to build on work that is already done.
Not so common? As powerful as it is, it is also only really useful in solving certain problems; in fact many OO programs never make us of it in any explicity way.
GUI systems are one of the common areas where inheritance is used to great benefit. Most GUI systems have an extensive hierarchy of inheritance.
Topics to be covered
- Defining - extends
- Overriding - @Override
- Preventing - final classes and methods. private
- Visibility - protected.
- Referencing - super.
- Issues - Coupling.
- Issues -
- Issues - Open vs closed desgin.
- Frequency - Less common than has-a and uses-a.
- Linguistic - "is-a" Every X is-a Y. An X is-a Y. Every X is a specialized Y.
- Terminology - subclass-superclass, child-parent, derived-base.
Java features
Java has many feature for defining inheritance, referencing inherited classes, controlling inheritance, etc.
- Inheritance is specified with the
extends
keyword.
Controversy - Open inheritance vs closed inheritance
One issue when designing source code that might be used by others is whether you should allow them to subclass your classes.
See DesignedInheritance for Martin Fowler's discussion of this issue.