Objects, Classes, and Interfaces |
As mentioned previously, when you define a new interface you are in essence defining a new reference data type. You can use interface names anywhere you'd use any primitive data type name or any other reference data type name.Consider the spreadsheet problem introduced on Creating and Using Interfaces. Let's say that you defined an interface named
CellAble
that looked something like this:Now, suppose that you had a row and column object that contained a bunch of objects that implemented theinterface CellAble { void draw(); void toString(); void toFloat(); }CellAble
interface. YourRow
class'ssetObjectAt
method could be implemented as in the following code example:Note the use of the interface nameclass Row { private CellAble[] contents; . . . void setObjectAt(CellAble ca, int index) { . . . } . . . }CellAble
in the member variable declaration forcontents
and the method parameter declaration forca
. Any object that implemented theCellAble
interface, regardless of where it existed within the class hierarchy, could be contained in thecontents
array and could be passed into thesetObjectAt
method.
Objects, Classes, and Interfaces |