Object-Oriented Java · 9 min read · Beginner
Interfaces & Generics
Write decoupled, compile-time-safe Java APIs using interfaces, @Override, and generic type parameters (<T>).
What you'll learn
- Define contracts with interface and implement them with implements
- Always annotate overridden methods with @Override
- Write type-safe reusable containers and methods with generics (<T>)
In Java, a class can extend only one parent class, but it can implement many interfaces. Interfaces let you decouple callers from concrete implementations and make unit testing straightforward.
Generics (<T>)
Generics let classes like List<String> or Repository<User> enforce element types at compile time, eliminating unsafe runtime casts.
Fill-in-the-Blank Exercise
Implement an Interface
Make EmailNotifier implement Notifier and mark send() with @Override.
interface Notifier {
void send(String msg);
}
class EmailNotifier Notifier {
public void send(String msg) {
System.out.println("Email: " + msg);
}
}Interactive Code Lab
JavaGenericsDemo.java
Expected OutputClick “Run Live” to compile with OpenJDK 21
[JAVA CERTIFIED]
Check Your Understanding
Score: 0 / 1
1. How many interfaces can a single Java class implement?
Study Workspace
Finished this lesson?
Your progress is saved locally in your browser, no account needed.