Feeds

Code inheritance and reuse: a delicate balancing act

The pot of gold at the end of many rainbows

Top 5 reasons to deploy VMware with Tegile

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

Security for virtualized datacentres

Next page: Compositional reuse

More from The Register

next story
PEAK APPLE: iOS 8 is least popular Cupertino mobile OS in all of HUMAN HISTORY
'Nerd release' finally staggers past 50 per cent adoption
Microsoft to bake Skype into IE, without plugins
Redmond thinks the Object Real-Time Communications API for WebRTC is ready to roll
Microsoft promises Windows 10 will mean two-factor auth for all
Sneak peek at security features Redmond's baking into new OS
Mozilla: Spidermonkey ATE Apple's JavaScriptCore, THRASHED Google V8
Moz man claims the win on rivals' own benchmarks
Yes, Virginia, there IS a W3C HTML5 standard – as of now, that is
You asked for it! You begged for it! Then you gave up! And now it's HERE!
FTDI yanks chip-bricking driver from Windows Update, vows to fight on
Next driver to battle fake chips with 'non-invasive' methods
DEATH by PowerPoint: Microsoft warns of 0-day attack hidden in slides
Might put out patch in update, might chuck it out sooner
Ubuntu 14.10 tries pulling a Steve Ballmer on cloudy offerings
Oi, Windows, centOS and openSUSE – behave, we're all friends here
prev story

Whitepapers

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.
Getting started with customer-focused identity management
Learn why identity is a fundamental requirement to digital growth, and how without it there is no way to identify and engage customers in a meaningful way.
Reg Reader Research: SaaS based Email and Office Productivity Tools
Read this Reg reader report which provides advice and guidance for SMBs towards the use of SaaS based email and Office productivity tools.
Saudi Petroleum chooses Tegile storage solution
A storage solution that addresses company growth and performance for business-critical applications of caseware archive and search along with other key operational systems.
Internet Security Threat Report 2014
An overview and analysis of the year in global threat activity: identify, analyze, and provide commentary on emerging trends in the dynamic threat landscape.