Collections and Interfaces
What is an interface? An interface is a contract. An interface consists of a collection of method headers.
An interface is also a data type and you can declare varialbes
of interface type and you can use interface types as type
parameters in generic classes such as ArrayList<T>.
This magical property is called polymorphism. It does not mean looking like this.
It means "having multiple shapes." A variable of interface type can point at.
Who signs the contract? A class can sign
an interface contract using the keyword implements.
How is this reflected in the documentation?
Open the package java.util. Scroll down to the
class list and hit the Interfaces tab. Now open the
Runnable interface. It specifies one method,
public void run().
Under All Known Implementing classes you will see a set of classes
that implement Runnable.
Interfaces can be Generic Some interfaces require
a type parameter. Open the interface Comparable.
You will see this at the top.
Module java.base
Package java.lang
Interface Comparable<T>
Type Parameters:
T - the type of objects that this object may be compared to
You will see that this interface is implemented by a ton
of classes, which include String and BigInteger.
It specifies one method, public int compareTo(T t).
Any object you intend to put in a collection and sort should implement this interface.