Interface with two implementations
Classes & objects · Beginner · 14 min · Runs in your Salesforce Developer org · Free
Define a `Notifier` interface with one method, then write two classes that implement it. `EmailNotifier` prefixes the message with `EMAIL: `, and `SlackNotifier` prefixes it with `SLACK: `. Each class provides its own body for the same method signature.
Lesson
Why this matters
Salesforce code often has to support more than one channel: email, Slack, chatter, SMS. If callers depend on a concrete class (EmailNotifier), swapping channels means rewriting them. An interface is a contract that says "anything with a send(String) method is a Notifier." Callers depend on the contract, not on any one class.
The smallest working example
public interface Notifier {
String send(String message);
}
public class EmailNotifier implements Notifier {
public String send(String message) {
return 'EMAIL: ' + message;
}
}
new EmailNotifier().send('Hi') returns EMAIL: Hi.
Name the parts
- **
interface Notifier** lists method signatures with no body. The semicolon aftersend(String message);ends the signature. - **
implements Notifier** on a class is a promise to provide every method on the interface. - Interface methods have no modifier. Inside the class, the exposed method must be **
public**. - Each class picks its own prefix and returns the result; the interface itself knows nothing about prefixes.
A common wrong version
public interface Notifier {
public String send(String message); // modifier is not allowed
}
public class EmailNotifier implements Notifier {
String send(String message) { // missing `public`
return 'EMAIL: ' + message;
}
}
Two mistakes. The interface method must have no modifier, and the class method must be public so callers can reach it.
The fix
Strip the modifier from the interface signature and mark the class method public:
public interface Notifier {
String send(String message);
}
public class SlackNotifier implements Notifier {
public String send(String message) {
return 'SLACK: ' + message;
}
}
Now you try
Fill in Notifier with one method String send(String message);. Make EmailNotifier and SlackNotifier return the message with the matching prefix. Keep every type name and signature; the grader creates instances by name and calls send.
Learn more
The challenge
Finish the Notifier interface and its two implementations.
Examples:
new EmailNotifier().send('Hi')returnsEMAIL: Hinew SlackNotifier().send('Hi')returnsSLACK: Hinew EmailNotifier().send('')returnsEMAIL:
Requirements:
new EmailNotifier().send('Hi')should beEMAIL: Hi.new SlackNotifier().send('Hi')should beSLACK: Hi.new EmailNotifier().send('')should beEMAIL:.new SlackNotifier().send('Deploy done')should beSLACK: Deploy done.new EmailNotifier().send('Order #42')should beEMAIL: Order #42.
Starter code
public interface Notifier {
}
public class EmailNotifier implements Notifier {
}
public class SlackNotifier implements Notifier {
}
Related drills
You write the Apex yourself. ApexSensei runs it and tells you what passed and what did not.
‹ Reject an invalid discount · virtual, override and super ›