Example 3-Overriding an Inherited Method

An important aspect of OOP programming is the ability of a subclass to replace an inherited method implementation with a new implementation especially appropriate to its instances. This is called overriding llo"q. a method. Sometimes, a class may introduce methods that every descendant class is expected to override. For example, SOMobject ves introduces the somPrintSelf method, and a good SOM programmer will generally override this method when implementing a new class.

The purpose of somPrintSelf is to print a brief description of an object. The method can be useful when debugging an application that deals with a number of objects of the same class -assuming the class designer has overridden somPrintSelf with a message that is useful in distinguishing different objects of the class. For example, the implementation of somPrintSelf provided by SOMobjects simply prints the class of the object and its address in memory. SOMclass overrides this method so that, when somPrintSelf is invoked on a class object, the name of the class will print.

This example illustrates how somPrintSelf might be overridden for the "Hello" class. An important identifying characteristic of "Hello" objects is the message they hold; thus, the following steps illustrate how somPrintSelf could be overridden in "Hello" to provide this information.


[Back: Attributes vs instance variables]
[Next: Example 4 - Initializing a SOM Object]