Overload a Case label method
Think in classes · Intermediate · 15 min · Runs in your Salesforce Developer org · Free
Scenario. A Case list needs one easy-to-read label method that accepts different kinds of detail.
Give one method several parameter lists for normal, urgent, and aged Cases.
What you will learn
Overloading means one method name with different parameter lists. Apex picks the version whose parameters match the arguments you pass.
String label(String subject)
String label(String subject, Boolean urgent)
One overload can call another, like return label(subject);, to reuse its logic.
Gotcha. Changing only the return type does not create an overload. The parameter types must differ.
The task
Overload a Case label method so each parameter list handles normal, urgent or aged Cases.
Examples:
label('Password reset')→ printed asDefault: Normal: Password resetlabel('Data loss', true)→ printed asWith flag: Urgent: Data loss. Withfalseit returnsNormal: Data loss.label('Pending refund', 7)→ printed asWith age: Aged: Pending refund. With6it returnsNormal: Pending refund.
Requirements:
- Keep
label(String subject). It returnsNormal:plus the subject. - Make the Boolean version return
Urgent:plus the subject when the flag is true, and the normal label when it is false. - Add an Integer version that returns
Aged:plus the subject at 7 days or more, and the normal label below 7. - Print
Default:,With flag:andWith age:, each followed by the matching call from the examples. - The checks also call
new CaseLabeler().label(...)with other inputs, so do not hard-code any answer.
Starter code
class CaseLabeler {
public String label(String subject) {
return 'Normal: ' + subject;
}
public String label(String subject, Boolean urgent) {
return label(subject);
}
}
CaseLabeler labeler = new CaseLabeler();
System.debug('Default: ' + labeler.label('Password reset'));
When it passes
Apex picks each overload by its arguments. Urgent and aged Cases get their own label, and everything else falls back to the normal label, including exactly 6 days.
You write the Apex yourself. ApexSensei runs it and tells you what passed and what did not.
‹ Expose a read-only quote total · Route Cases with an enum ›