- the collection of the methods and constants of a class can be
captured in a Java construct called an interface
public interface Teacher {
/* each of these methods is public and abstract, and that is implicit
* so does not need to be shown in the interface
*/
/* @param start The time at which teaching begins
@param finish The time at which teaching stops
@return The contents of the teaching
*/
Voice teach(Time start, Time finish);
/* @param question The subject that needs to be clarified
@return The clarification
*/
Voice askClarification(Voice question);
int numberOfExams = 3; /* implicitly "public abstract final" */
}
- a Java class can then claim to implement such an interface:
public class Edo implements Teacher { ...
public class John implements Teacher { ...
The compiler will report an error if the class fails to
provide the needed methods and constants.
- code can then use these classes interchangeably, if all that is
needed is the methods and constants specified by the interface:
Teacher lectureTeacher = new Edo();
Teacher labTeacher = new John();
Student I = new ...();
try {
try {
I.listen(lectureTeacher.teach(10:30, 11:45));
} catch (NotUnderstood something) {
I.listen(lectureTeacher.askClarification(something));
}
} catch (MissingInformation somethingElse) {
/* MissingInformation could be from the teaching or the clarification */
I.listen(labTeacher.askClarification(somethingElse));
}