Java Reference
In-Depth Information
As you did with the application HelloWorld, the applet was extended to con-
tain two instances of
ErrorMsg
, each with its own data members:
ErrorMsg myErrorMsg2 = new ErrorMsg ();
myErrorMsg2.setErrorMsg ("Some Text for #2");
To show that two unique data members with the same name exist in Hello-
World (one for each object of type
ErrorMsg
), you displayed both data mem-
bers. The one associated with
myErrorMsg2
contained “Some Text for #2,” and
the one associated with
myErrorMsg
contained “Some Text.”
tempMsg = myErrorMsg2.getErrorMsg ();
g.drawString (tempMsg, 5, 75);
tempMsg = myErrorMsg.getErrorMsg ();
g.drawString (tempMsg, 5, 85);
Finally, you showed how
ErrorMsg
could define two variations on the same
method. This is known as
method overloading
. The new variation accepted one
character argument and is the argument that was set to the character value
'U'
that represents the case flag to tell the method to convert the return message to
all uppercase.
// Define a variation on the public method getErrorMsg.
public String getErrorMsg (char caseFlag) {
String returnMsg;
// Set the local variable returnMsg to the data member msgText.
returnMsg = msgText;
// Define a variation on the public method getErrorMsg.
// Perform the standard 'getErrorMsg' method.
public String getErrorMsg (char caseFlag) {
// Convert to all upper case, if requested.
if (caseFlag == 'U')
return (getErrorMsg().toUpperCase ());
else
// Return from this method, without conversion.
return (getErrorMsg());