Java Reference
In-Depth Information
desired property of “additive, not invasive” change that we mentioned earlier in this
chapter.
It may seem odd that we can have interface variables, arrays, and parameters when
it isn't possible to construct an object of an interface type, but all this capacity means
is that any object of a type which implements that interface may be used. In our case,
any type that implements Shape (such as Circle , Rectangle , or Triangle ) may be
used.
Also recall that interfaces help us cope with the limitations of single inheritance. A
class may extend only one superclass but may implement arbitrarily many interfaces.
The following is the general syntax for headers of classes that extend a superclass
and implement one or more interfaces:
public class <name> extends <superclass>
implements <interface>, <interface>, ..., <interface> {
...
}
Many classes in the Java class libraries both extend a superclass and implement
one or more interfaces. For example, the PrintStream class (of which System.out
is an instance) has the following header:
public class PrintStream extends FilterOutputStream
implements Appendable, Closeable, Flushable
9.6 Case Study: Financial Class Hierarchy
As you write larger and more complex programs, you will end up with more classes
and more opportunities to use inheritance and interfaces. It is important to practice
devising sensible hierarchies of types, so that you will be able to solve large problems
by breaking them down into good classes in the future.
When you are designing an object-oriented system, you should ask yourself the
following questions:
What classes of objects should I write?
What behavior does the client want each of these objects to have?
What data do the objects need to store in order to implement this behavior?
Are the classes related? If so, what is the nature of the relationships?
Having good answers to these questions, along with a good knowledge of the nec-
essary Java syntax, is a good start toward designing an object-oriented system. Such a
process is called object-oriented design . We discussed some object-oriented design
heuristics in the case study of Chapter 8.
 
Search WWH ::




Custom Search