Java Reference
In-Depth Information
Add the paint() Method
Let's add the paint() method to the Rectangle class so that it compiles properly.
Add the paint() method shown in Figure 10.5, and compile your Rectangle
class again. It should compile successfully this time.
You have now written both an interface and a class that implements the
interface. Next, you will write a class that paints Paintable objects, and we will
use the Rectangle class as an example.
Write a Class That Uses Paintable
Write the MyPainter class in Figure 10.6, which contains a paintAnything()
method. The paintAnything() method can paint any object of type Paintable.
Within main(), a MyPainter object paints a Rectangle object passed in to the
paintAnything() method. Type in the MyPainter program and save it in your
c:\interface directory.
Run the MyPainter program, and you should see output similar to that in
Figure 10.7.
Figure 10.5
The Rectangle class properly implementing Paintable.
Search WWH ::




Custom Search