Subclassing

As already mentioned, the object-oriented approach facilitates the reuse of application code; generic objects may be created and used by multiple applications to perform actions upon the same data object or type of object. However, one of the criticisms often levelled at this capability is that it becomes impossible to foretell the total set of actions that may ever be required with respect to a particular data object, and that an object is therefore never truly reusable.

The object-oriented approach overcomes this difficulty by providing a way for applications to modify the behavior of existing application objects without modifying the objects themselves; this is known as subclassing the object. A subclass application object is composed of a data object definition and a certain number of methods to manipulate that object. The subclass application object may contain methods that are not contained within the original application object created for that data object, or methods that are modified in some way from those contained in the original object. In this way a subclass application object may add new methods to perform actions which are not performed by the original object class, or to handle certain types of message in a different way than that normally carried out by the original object class.

Figure "Subclassing an Application Object"

When an application object has been subclassed, all messages intended for that object are directed to the subclass application object first. The sending object need not be aware that the message has been diverted. If the subclass application object does not contain a method to deal with a particular message, it should then pass the message on to the original application object, for processing by one of that object's methods. The original application object receiving a message in this way should also be unaware that it has been subclassed.

A useful application of the subclassing principle occurs when a generic application object is defined and stored away for use by many applications, taking advantage of the reusability aspects of the object-oriented approach. Where one application wishes to perform a particular action in a slightly different manner than that performed by the methods associated with the generic object, a subclass application object may be created containing a new method for that specific action only, and passing all other messages to the original application object for regular processing. It can be seen that subclassing is a technique for application of the concept of inheritance, through its ability to transparently add properties and methods to existing objects.

Subclassing also provides a way to overcome the danger of inadvertently impacting the behavior of other applications by modifying an existing application object. If a standard is adopted whereby existing application objects in a production environment may only be modified through subclassing, such changes do not impact applications using the original object or which may themselves have subclassed that object. In this way the need for regression testing of affected applications is eliminated, and the degree of object-management required is significantly reduced.

When a functional requirement may be satisfied by modifying the methods of an existing application object (through subclassing), a decision must be made regarding the relative merits of modifying the object, against creating a new object. Various texts advocate a rule whereby a new object should be created when more than 10% or 20% of an existing object's methods must be modified. However, the decision of whether to modify an existing object or create a new object must be taken on the basis of object complexity, degree of modification and experience.


[Back: Class-Based vs Module-Based]
[Next: User View vs Application View]