Contractual Interfaces
What is an Interface?
An interface defines a contract between classes that may choose to implement it. It declares behaviors that implementing classes must define in their own terms. This allows users (i.e., programmers) of the class to write code with confidence that the class will support certain behaviors out-of-the-box.
Let's take a look at a popular interface called Measurable
.
public interface Measurable {
double getMeasure(); // methods are automatically public
}
Any class that implements the Measurable
interface needs to implement the getMeasure
method.
public class BankAccount implements Measurable {
public double getMeasure() {
return balance;
}
}
Classes can implement more than one interface, but can only extend a single class (an important difference between abstract classes and interfaces).
Interfaces can also be used as data types, where they represent any class that implements it (in a way, this is like polymorphism).
public double getSum(Measureable[] objs) {
double sum = 0;
for (Measurable obj : objs) {
sum = sum + obj.getMeasure();
}
return sum;
}
The Comparable
Interface
Comparable
InterfaceComparable
is a built-in interface in Java that defines a single method: compareTo
. The compareTo
method will compare two objects of the same class and return a positive value, 0, or a negative value. This corresponds to whether the first object is greater than, equal to, or less than the second object, respectively. We've seen this used with String
s in earlier units.
Our job, as programmers and implementers of the Comparable
interface, is to define what it means for an object to be greater than, equal to, or less than another object. For something like a Card
class, we might look to the rank (and potentially the suit, depending on the game) to determine how to order these objects.
Once we've implemented the Comparable
interface, we can automatically sort lists of the implementing class (in the case, the Card
class).
public class Card implements Comparable<Card> {
// note the type being compared in the angled brackets above
// some Card class implementation details omitted
@Override
public int compareTo(Card card) {
if (this.getRank() > card.getRank()) {
return 1;
} else if (this.getRank() < card.getRank()) {
return -1;
} else {
return 0;
}
}
}
Card
s can now be sorted (perhaps in a Hand
), since we've defined the manner in which ordering should take place.
Last updated
Was this helpful?