Java Reference
In-Depth Information
Because
Point
and
Circle
instancesaredrawablesbyvirtueoftheseclassesim-
plementingthe
Drawable
interface,itislegaltoassign
Point
and
Circle
instance
references to variables (including array elements) of type
Drawable
.
When you run this method, it generates the following output:
Point drawn at (10, 20) in color 1
Circle drawn at (10, 20) with radius 30 in color 1
youalsoneedtofillashape'sinterior.Youmightattempttosatisfythisrequirementby
Listing 2-44.
Declaring a
Fillable
interface
interface Fillable
{
int RED = 1;
int GREEN = 2;
int BLUE = 3;
int BLACK = 4;
void fill(int color);
}
implementbothinterfacesbyspecifying
class Point implements Drawable,
Fillable
and
class Circle implements Drawable, Fillable
. You
canthenmodifythe
main()
methodtoalsotreatthedrawablesas
fillables
sothatyou
can fill these shapes, as follows:
public static void main(String[] args)
{
Drawable[] drawables = new Drawable[] { new Point(10,
20),
new
Circle(10,
20, 30) };
for (int i = 0; i < drawables.length; i++)
drawables[i].draw(Drawable.RED);
Fillable[] fillables = new Fillable[drawables.length];
for (int i = 0; i < drawables.length; i++)
{