Java Reference
In-Depth Information
Chapter 26. Introducing Extensions
An extension is the addition of a class, an interface, or a method to an existing code base.
The most common way to extend code is to write a new class, although you can also extend
a class through delegation. In particular, when you'd like to subclass from two parents, you
can subclass from one parent and use delegation to "inherit" the behavior of the other class.
Object-oriented development in Java does not begin from scratch. Rather, it's fair to say that
object-oriented software development is extension. To develop in Java, learn what is available
in an existing code base, including a company's code and the Java class libraries. After seeing
where your changes fit in, add the code you need for your application. In Java, application
development is extension, and extension begins where reuse leaves off.
Reuse as an Alternative to Extension
When you shop around before you code, you have less code to maintain, you benefit from
the wisdom of developers who have gone before you, and you can ensure that your own code
dovetails with the class you are extending. Every class that you create will inherit from the
Object class, and this is a natural place to start shopping for existing code. Figure 26.1
shows the Object class.
Figure 26.1. The Object class has methods that every OO developer should know how to
use.
Earlier examples in this topic have reused most of the behavior available in Object ,
including the clone() , equals() , and getClass() methods. As a Java developer, you
should be able to reuse the wait() and notify() methods that Object provides.
Suppose that the Oozinoz robot can stack bins of material but only to a height of, say, three
bins. If you command the robot to stack a fourth bin, it will wait until a human removes a bin.
Search WWH ::




Custom Search