Interfaces in Java
Today's notes - Interfaces
Terminology
- interface
- An interface specifies
behavior for a class. - Gaddis definition
It is also the keyword that
defines the interface
- implements
- the java keyword that
indicates that a class is implementing a specified interface
Notes
- An interface provides a contract for a class's behaviors.
- interfaces provide method
headers. May also provide constants. May not provide attributes
(because these are not inherited).
- a class implementing an
interface must implement each of the interface's methods...this is the
contract.
- interfaces provide a
"multiple inheritance" mechanism. It is not inheritance, but classes
that implement the same interface can be treated as if they share the
"is-a" relationship.
- interfaces may be extended.
The method headers (contractual requirements) are inherited.
Implementing classes must implement all methods in the child and all
ancestors.
- interface methods are always public.
Syntax
declaration
visibility interface
name
using
visibility class
name implements
interfaceName
Examples
Relatable.java - The interface
FinalExam3.java - The implementing
class
InterfaceDemo.java -
The driver
GradedActivity.java - The
parent class
Relatable.pdf - UML document
In class activity: Worksheet.doc
Arrays.sort
& the Comparable
interface.
Implementing Comparable: GradedActivity2
Final example:
GradedActivity.java
FinalExam3.java
PassFailActivity.java
PassFailExam.java
Tester.java
Relatable.java
CS
239 - Fall 2006