Feeds

Code inheritance and reuse: a delicate balancing act

The pot of gold at the end of many rainbows

Secure remote control for conventional and virtual desktops

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.

Inheritance-oriented 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).

The essential guide to IT transformation

Next page: Compositional reuse

More from The Register

next story
The Return of BSOD: Does ANYONE trust Microsoft patches?
Sysadmins, you're either fighting fires or seen as incompetents now
China hopes home-grown OS will oust Microsoft
Doesn't much like Apple or Google, either
Linux turns 23 and Linus Torvalds celebrates as only he can
No, not with swearing, but by controlling the release cycle
This is how I set about making a fortune with my own startup
Would you leave your well-paid job to chase your dream?
Microsoft cries UNINSTALL in the wake of Blue Screens of Death™
Cache crash causes contained choloric calamity
Eat up Martha! Microsoft slings handwriting recog into OneNote on Android
Freehand input on non-Windows kit for the first time
Linux kernel devs made to finger their dongles before contributing code
Two-factor auth enabled for Kernel.org repositories
prev story

Whitepapers

Implementing global e-invoicing with guaranteed legal certainty
Explaining the role local tax compliance plays in successful supply chain management and e-business and how leading global brands are addressing this.
5 things you didn’t know about cloud backup
IT departments are embracing cloud backup, but there’s a lot you need to know before choosing a service provider. Learn all the critical things you need to know.
Why and how to choose the right cloud vendor
The benefits of cloud-based storage in your processes. Eliminate onsite, disk-based backup and archiving in favor of cloud-based data protection.
Top 8 considerations to enable and simplify mobility
In this whitepaper learn how to successfully add mobile capabilities simply and cost effectively.
High Performance for All
While HPC is not new, it has traditionally been seen as a specialist area – is it now geared up to meet more mainstream requirements?