|
|
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
CellAblethat 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(); }CellAbleinterface. YourRowclass'ssetObjectAtmethod 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) { . . . } . . . }CellAblein the member variable declaration forcontentsand the method parameter declaration forca. Any object that implemented theCellAbleinterface, regardless of where it existed within the class hierarchy, could be contained in thecontentsarray and could be passed into thesetObjectAtmethod.
|
|
Objects, Classes, and Interfaces |