Java Reference
In-Depth Information
This technique can be very useful as a mechanism for defining complex sub-
routines, that is, those whose functions can be extended by calling programs. For
example, a set of complex subroutines might serve as a framework for processing
multiple items in a list. The items in the list could be rows read in from a database.
The list processing framework might provide standard list-management functions,
such as searching and positioning. In this case, the basic control logic necessary to
process a list is in the subroutine, and the calling program provides whatever spe-
cific processing is required.
With the ability to extend the subroutine built right into the subroutine, it is
often possible that these types of routines can be used (reused, actually) to meet
new requirements. To make this technique useful, it is important that the subrou-
tine be coded so that it returns to the caller at key points, indicating that the func-
tion is not complete. The caller can then perform any specialized code required.
However, a word of caution here: The coding technique necessary to support
this mechanism in MYSUB is very ugly and is recommended only to the coura-
geous COBOL programmer. Further, if it is appropriate to create a front end to
MYSUB (like NEWSUB), the ugliness must be promoted into NEWSUB. In that
case, NEWSUB must define and manage an interface (i.e., a LINKAGE SECTION)
very similar to MYSUB so as to allow the return from the MYSUB subroutine to
percolate back through NEWSUB into CALLER.
In summary, the concept of inheritance, although it is possible to implement in
COBOL, can be difficult to implement and requires a very serious commitment to
code reuse. In contrast, Java makes these types of designs readily available and di-
rectly supported by the language, which also makes it incredibly easy and intuitive.
I NHERITANCE AND J AVA
As I've just noted, Java makes implementation of these concepts much easier, even
fundamental. New classes can naturally extend existing classes. Developers need
not worry about the program flow and interface specification issues I have dis-
cussed, since the compiler handles all of that automatically. Developers are en-
couraged to reuse existing class definitions to meet new needs—in some cases by
modifying the base classes, in others by creating specialized classes that inherit the
capabilities of the base classes. In Java, code should never be copied to reproduce or
modify an existing class's capabilities.
Java's syntax to define inheritance is the keyword extends . In the previous ex-
amples, ErrorMsg extended the class TextMessage . To add an additional level of in-
heritance, the example now has ErrorMsg extended by the classes PopupErrorMsg
and PrintfileErrorMsg (see Figure 5.6).
Search WWH ::




Custom Search