Java Reference
In-Depth Information
// Create an instance of the helper class.
WriteLineImpl writeLineImpl = new WriteLineImpl();
...
public void printLine (String inputMsg) {
// Use the helper class method to implement this method.
writeLineImpl. printLine (inputMsg);
return;
}
}
This combination of interfaces and supporting classes is often used within a
package called a framework . A good example of a framework is the collections frame-
work first provided with Java 1.2. I will review the collections frameworks in some
detail in Chapter 11.
H IDING M ETHODS AND M EMBERS
Derived classes have to be careful with the use of the static keyword when overrid-
ing methods in its superclasses. Instance methods (i.e., nonstatic methods) cannot
override static methods in the superclass, and static methods cannot override in-
stance methods in the superclass.
If a derived class declares a method as static, then any static methods with the
same signature in the superclass(es) will not be readily available to the derived class.
The superclass method can be accessed explicitly by using the super keyword or an
explicit reference to the superclass type (e.g., TextMessage.staticMethod ). If a de-
rived class tries to override an instance method with a static method, the compiler
will complain. Finally, a static method in a superclass cannot be overridden with an
instance method in the derived class.
On the other hand, a derived class can hide variables in the superclass, using the
static keyword. If a derived class declares a variable as static, then any instance variables
with the same name in the superclass(es) will not be readily available to the derived
class. The superclass variables can be accessed explicitly by using the super keyword
but not with an explicit reference to the superclass type (e.g., TextMessage.static-
Variable ). However, a derived class can override an instance method with a static
method, and vice versa, without causing the compiler to complain.
Search WWH ::




Custom Search