Lesson 4 of 9
Interfaces - Contracts & Multiple Behaviour
An interface is a pure contract - it defines what a class can do without specifying how. Since Java 8, interfaces can have default and static methods, enabling interface evolution without breaking existing implementations. A class implements multiple interfaces - the primary mechanism for achieving multiple behaviour in Java.
Interface Features (Java 8+)
- Abstract methods (implicitly public abstract) - must be implemented by classes
- Default methods - concrete implementations in the interface (use for backward compatibility)
- Static methods - called on the interface itself, not on instances
- Constants - fields are implicitly public static final
- Private methods (Java 9+) - helper methods within the interface
- Functional interface - has exactly ONE abstract method; can be used as lambda target
Interfaces in the Library System
// Interface: what can be searched
public interface Searchable {
boolean matchesQuery(String query);
// Default method - shared convenience built on the abstract contract
default boolean matchesQueryIgnoreCase(String query) {
return matchesQuery(query.toLowerCase());
}
}
// Interface: what can be rated
public interface Rateable {
void addRating(int rating); // 1-5 stars
double getAverageRating();
default String getRatingLabel() {
double avg = getAverageRating();
if (avg >= 4.5) return "Outstanding";
if (avg >= 3.5) return "Good";
if (avg >= 2.5) return "Average";
return "Poor";
}
// Static factory method on interface
static int validateRating(int rating) {
if (rating < 1 || rating > 5) throw new IllegalArgumentException("Rating must be 1-5");
return rating;
}
}
// Book implements BOTH interfaces
public class Book extends LibraryItem implements Searchable, Rateable {
private final String author;
private double price;
private final java.util.List<Integer> ratings = new java.util.ArrayList<>();
// ... constructors, other methods ...
// Implements Searchable
@Override
public boolean matchesQuery(String query) {
String q = query.toLowerCase();
return getTitle().toLowerCase().contains(q)
|| author.toLowerCase().contains(q);
}
// Implements Rateable
@Override
public void addRating(int rating) {
ratings.add(Rateable.validateRating(rating)); // call interface static method
}
@Override
public double getAverageRating() {
return ratings.isEmpty() ? 0.0 :
ratings.stream().mapToInt(Integer::intValue).average().orElse(0.0);
}
}
// polymorphism via interface reference
Searchable s = new Book("ISBN", "Effective Java", "Joshua Bloch", 44.99);
System.out.println(s.matchesQuery("bloch")); // trueQuick Quiz
Tip
Tip
Practice Interfaces Contracts Multiple Behaviour in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Visual architecture and concepts for Interfaces - Contracts & Multiple Behaviour
Practice Task
Note
Practice Task - (1) Write a working example of Interfaces Contracts Multiple Behaviour from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Common Mistake
Warning
A common mistake with Interfaces Contracts Multiple Behaviour is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready java code.
Key Takeaways
- An interface is a pure contract - it defines what a class can do without specifying how.
- Abstract methods (implicitly public abstract) - must be implemented by classes
- Default methods - concrete implementations in the interface (use for backward compatibility)
- Static methods - called on the interface itself, not on instances
Finished this lesson?