
Patrick B. answered 07/13/19
Math and computer tutor/teacher
An interface is used to create a GENERIC routine so that it does the same operations, functionality, calculations, processes, etc. The only thing they differ by are the data types.
For example, consider the QuickSort routine. The functionality, implementation, algorithm,
procedures, and routine as a whole does NOT depend on the data it is sorting...
the pivoting is done the same way, the swapping is done the same way, and so is the recursion.
So to create such a generic routine to accomodate any type of data, one creates an INTERFACE that does the comparison. This interface is passed to the quicksort routine so that it can compare that particular data type.
Specifically, the programmer declares the interface Sortable which contains a method:
int Compare( Object x, Object y); // returns 1 if x>y, -1 if x<y, 0 if x=y
Now, class IntSorter implements Sortable, and MUST contain a method called Compare
which compares two integers.
SImilarly , class DataRecSorter implements Sortable and MUST contain a method called Compare
which compares two dataRecs.
These interfaces are passed to the quickSort routine, so it can sort any data type you want.
If you are familiar with C/C++, an interface does the same thing as a function pointer.
Base classes are used declare a generic class, where each descendant adds more detail.
EX.
class FELINE <--- cat <-- tabbies, simese, short_hair, long_hair, etc
lion <-- simba, rasta,
tiger <--- jaguar, puma
class Quadrilateral <--- parallelogram <--- square
<--- rectangle
<--- trapezoid