Java Reference
In-Depth Information
FIGURE 5.2
An object from the ErrorMsg class contains any members from its superclass(es). Its
public interface is the sum of all of the public methods it and its superclass(es) define.
C ALLER C LASS
A class that uses this type of derived class simply needs to create the ErrorMsg ob-
ject. The TextMessage object will be automatically created. For example:
// Create an instance of ErrorMsg.
// This will automatically create a new instance of ErrorMsg,
// which will contain the members of the base class TextMessage.
ErrorMsg myErrorMsg = new ErrorMsg ();
// Call the translate method in the base class TextMessage, using the
// ErrorMsg object reference variable.
String FrenchText = myErrorMsg.getTranslation ();
Notice that the caller program creates only the subclass ( ErrorMsg ) and does not
need to explicitly create the superclass ( TextMessage ). Yet it still can call the parent
class method ( getTranslation ), using the reference variable ( myErrorMsg ) of the sub-
class. The caller class simply needs to create the subclass; all of the public methods of
any inherited base class(es) are instantly available. Likewise, all data members of
TextMessage (both public and private data members) are created at the same time.
Public data members in TextMessage can be directly accessed by the caller class.
One of the first things that happens when an object is created is that the system calls
every constructor in the base class(es) for this object. This is how the compiler
makes sure that every class “contains” its base class. The developer does not have
to consciously do this: The compiler and runtime system do it automatically.
Search WWH ::




Custom Search