Java Reference
In-Depth Information
FIGURE 5.3
In this definition of ErrorMsg , the derived class ( ErrorMsg ) overrides the
getTranslation method of its superclass ( TextMessage ). Yet the public interface to
ErrorMsg remains the same.
C ALLER C LASS
Notice that the way the consumer class uses ErrorMsg does not change, even though
you are performing a new function:
ErrorMsg myErrorMsg = new ErrorMsg ();
// Call the translate method in the subclass ErrorMsg.
String FrenchText = myErrorMsg.getTranslation ();
E XTENDING A M ETHOD
The getTranslation method in the derived class ( ErrorMsg ) could call the original
getTranslation method in the superclass (i.e., TextMessage ) if needed. This is often
necessary with derived methods in order to perform the original method, plus any
specific logic in the derived method. The keyword super is the reference variable for
the parent object (i.e., the object that was automatically created for this subclass).
E RROR M SG C LASS
This ErrorMsg class defines its own getTranslation method. This method will per-
form some specialized logic for the ErrorMsg class. It will still use the getTranslation
method in the base class to get the translated text, and then will convert the trans-
lated text to uppercase.
Search WWH ::




Custom Search