Feeds

Code inheritance and reuse: a delicate balancing act

The pot of gold at the end of many rainbows

Maximizing your infrastructure through virtualization

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

Reducing security risks from open source software

Next page: Compositional reuse

More from The Register

next story
HIDDEN packet sniffer spy tech in MILLIONS of iPhones, iPads – expert
Don't panic though – Apple's backdoor is not wide open to all, guru tells us
Do YOU work at Microsoft? Um. Are you SURE about that?
Nokia and marketing types first to get the bullet, says report
Microsoft takes on Chromebook with low-cost Windows laptops
Redmond's chief salesman: We're taking 'hard' decisions
Cheer up, Nokia fans. It can start making mobes again in 18 months
The real winner of the Nokia sale is *drumroll* ... Nokia
EU dons gloves, pokes Google's deals with Android mobe makers
El Reg cops a squint at investigatory letters
Chrome browser has been DRAINING PC batteries for YEARS
Google is only now fixing ancient, energy-sapping bug
Big Blue Apple: IBM to sell iPads, iPhones to enterprises
iOS/2 gear loaded with apps for big biz ... uh oh BlackBerry
prev story

Whitepapers

Seven Steps to Software Security
Seven practical steps you can begin to take today to secure your applications and prevent the damages a successful cyber-attack can cause.
Consolidation: The Foundation for IT Business Transformation
In this whitepaper learn how effective consolidation of IT and business resources can enable multiple, meaningful business benefits.
Designing a Defense for Mobile Applications
Learn about the various considerations for defending mobile applications - from the application architecture itself to the myriad testing technologies.
Build a business case: developing custom apps
Learn how to maximize the value of custom applications by accelerating and simplifying their development.
Consolidation: the foundation for IT and business transformation
In this whitepaper learn how effective consolidation of IT and business resources can enable multiple, meaningful business benefits.