Java Reference
In-Depth Information
In other OO languages, a class can simultaneously extend multiple base classes (this
is called multiple inheritance ). In Java, a class can extend only one base class. Any
Java class that does not explicitly extend another class is, by default, an extension of
the Java.lang.Object base class. So all classes ultimately inherit the class Object .
As previously discussed, any instance variables that belong to the base class are
contained in the derived class (assuming they are public). Also, public methods of
the base class become public methods of the derived class. Of course, the derived
class can define additional instance variables as well as new methods.
A Java class that extends another actually has a sort of dual identity: its parent
class identity and its own identity. Therefore, all members in the derived class (i.e.,
both new members defined only in this derived class and members derived from
the base class) are referenced in the same way by the calling program.
T EXT M ESSAGE C LASS
The following code snippets extend (gruesome pun intended) the previous class de-
finitions in order to highlight the Java syntax that supports and uses these concepts.
public class TextMessage {
public String msgText;
public int msgSize;
...
public void getTranslation () {
...
}
}
E RROR M SG C LASS
public class ErrorMsg extends TextMessage {
...
public void setErrorMsg (String inputMsg) {
...
}
}
Search WWH ::




Custom Search