Code inheritance and reuse: a delicate balancing act
The pot of gold at the end of many rainbows
There are two primary concepts that are used extensively within Java (and indeed other OO languages) to promote reuse - inheritance and componentisation.
For many people, inheritance is the obvious (and classic) example of how to achieve reuse within Java systems. However, the ability to plug components together is equally important. Here a component can be anything from a class that implements an interface and is "plugged together" with a class that uses the protocol defined by the interface to JavaBeans; to much larger components such as a JMS system, or transaction management system. The key is that the component is used as is, without any modification.
Typically, within the literature on languages such as Java, the focus is often on inheritance (particularly in tutorial or training/educational materials). However, in practice, the reuse of components can be equally (if not more) important.
One way to distinguish between inheritance-based reuse and compositional reuse is that inheritance-based reuse is primarily developer-oriented ("developer" here refers to those developing the functionality of the elements which might comprise a component); while compositional reuse is user-oriented (that is, no further development of the component takes place).
In the following we will briefly look at inheritance oriented reuse (and its role) and component based reuse.
Below we consider how classes can be modified through inheritance, and the dangers associated with this. Then, we propose the use of composition to promote reuse in some situations.
Categories of extension
When inheritance is used, an existing class (the superclass) is extended to create a new class (the subclass). The way in which it is extended can be categorised as modifications to the external protocol of the superclass; or as changes to the behaviour (implementation) of methods. These two categories are considered in more detail below.
Changes to the external protocol
When a subclass adds new methods that are available outside the class, it changes the external protocol of the superclass. This happens in a number of different situations:
- The subclass adds entirely new methods.
- The subclass provides convenience wrappers for existing methods.
- The subclass restricts methods provided by the superclass. This may include removing methods, or changing method parameters to types that are more restrictive. Currently, there is no way in Java to restrict the methods inherited from a class.
Changes in the implementation of the methods
If a subclass overrides a superclass's methods, then it modifies the behaviour of the superclass. Again, there are a number of different situations within which this can happen:
- The subclass provides a service for the superclass by overriding a method (structural inheritance).
- The subclass needs to perform additional actions when a method is called (functional inheritance).
- The subclass needs to replace the behaviour in a particular method (functional inheritance).
Sponsored: Benefits from the lessons learned in HPC