Java Reference
In-Depth Information
R
EDEFINING A
M
ETHOD
What if the developer of
ErrorMsg
needs to extend the
getTranslation
function of
TextMessage
in some fashion? In this case, the developer could simply define a func-
tion with that name in the class definition for
ErrorMsg
. Now, the
getTranslation
method in
ErrorMsg
will be performed by the caller class and not the
getTranslation
method in
TextMessage
.
E
RROR
M
SG
C
LASS
This
ErrorMsg
class defines its own
getTranslation
method. It will be used instead of
the
getTranslation
method in
TextMessage
when
ErrorMsg.getTranslation()
is called
(see Figure 5.3).
// This class extends the TextMessage class.
// Therefore, TextMessage is the superclass, and ErrorMsg is the subclass.
public class ErrorMsg extends TextMessage {
public void setErrorMsg (String inputMsg) {
...
// Some logic:
...
}
}
// Define a method named getTranslation. This overrides the method in the
// base class.
public String getTranslation () {
String localString;
...
// Some logic
...
return (localString);
}
}