Java Reference
In-Depth Information
That's all there is to it! Polymorphism is no more than the ability of multiple
class types to receive the same message and process it in unique ways.
E XERCISES
It's time to visit the example classes again and try out all these new ideas.
1. Using a text editor, edit a new Java source file and name it TextMessage.java
in the java4cobol directory. Enter this code in the class body. Note that the
two member variables are the same name as the member variables in
ErrorMsg . You will remove these variables from ErrorMsg in a moment.
/
//
// TextMessage
//
//
class TextMessage {
// Define some public class instance variables.
public String msgText;
public int msgSize;
public static char LANGUAGECODE = 'E';
public void setMsgText (String inputMsg) {
// Set the msgText and msgSize variables.
msgText = inputMsg;
// Set this variable to the size of the text String.
msgSize = msgText.length ();
}
public String getTranslation () {
// Perform the translation function for this message.
// In a production environment, the translation for this message might
// be accessed from a database.
// In this sample, you will return the original text
// and "French Text" as a generic translation.
if (LANGUAGECODE == 'E')
return (msgText);
else
return (msgText + " -➔ French Text");
}
}
Search WWH ::




Custom Search