An interface in Apex specifies a set of methods that implementing classes must provide. Interfaces enable a form of multiple inheritance, allowing a class to implement multiple interfaces. It is similar to a class, but it does not have any of its methods implemented. Instead, each method’s body is empty, and only the method signatures are present. It could be defined using the interface keyword, followed by the interface name and declare method signatures without providing the implementation.
Let’s consider a scenario where you are developing a custom Salesforce application for a university. In this application, you need to create a class that represents a student. The Student class should implement two interfaces: Enrollable and ScholarshipEligible.
First, let’s define the two interfaces:
void enroll();
}
public interface ScholarshipEligible { Boolean isEligibleForScholarship();
}
// Student Class Implementing Interfaces
public class Student implements Enrollable, ScholarshipEligible { private String name;
private Boolean enrolled;
private Boolean hasGoodGrades;
public Student(String name, Boolean hasGoodGrades) { this.name = name;
this.hasGoodGrades = hasGoodGrades;
this.enrolled = false;
}
public void enroll() {
enrolled = true;
System.debug(name + ‘ has been enrolled.’);
}
public Boolean isEligibleForScholarship() { return hasGoodGrades;
}
}
Student student2 = new Student(‘Jane Smith’, false); student2.enroll(); // Output: Jane Smith has been enrolled. Boolean isEligible2 = student2.isEligibleForScholarship(); System.debug(‘Is eligible for scholarship: ‘ + isEligible2); // Output: Is eligible for scholarship: false
In this code block, the interface definitions, the Student class, and the usage example are combined. You can execute this code as an anonymous Apex block in the Developer Console. This code will create Student objects, enroll them, and check their scholarship eligibility. The debug logs will display the output messages.
By implementing multiple interfaces, you can define different sets of behaviors for your classes and achieve more flexible and modular code.
Interface methods are implicitly public and cannot be modified. Implementing classes must adhere to this visibility. Both classes and interfaces are fundamental to building well-organized, modular, and maintainable applications on the Salesforce platform.
Apex includes a variety of method-related keywords such as static, virtual, global, abstract, and instance which empower developers to create efficient, modular, and extensible code:
Static Methods
–– A static method belongs to the class itself, not to instances of the class.
–– It can be called using the class name, without creating an instance of the class.
–– Static methods cannot access instance variables, but they can access other static variables or other static methods.
Let’s say you have a class called StringUtil that contains a static method called capitalizeFirstLetter. This method takes a string as input and returns the string with the first letter capitalized. Here’s how the code would look:
Step 1: Create the class
public class StringUtil {
public static String capitalizeFirstLetter(String input) { if (String.isBlank(input)) {
return input;
}
return input.substring(0, 1).toUpperCase() + input.substring(1);
}
}
Step 2: Save the class
Step 3: Open the anonymous window
Step 4: Test the public class
String capitalizedString = StringUtil.capitalizeFirstLetter(inputString); System.debug(‘Original String: ‘ + inputString); System.debug(‘Capitalized String: ‘ + capitalizedString);
To use this static method, you can call it directly on the class name, without creating an instance of the StringUtil class. The provided Apex code above capitalizes the first letter of a given string using a static method in the StringUtil class.
This is just one example of how you can use Apex static methods. They are flexible and can be used in various scenarios to perform specific tasks without the need to create an instance of the class.An interface in Apex specifies a set of methods that implementing classes must provide. Interfaces enable a form of multiple inheritance, allowing a class to implement multiple interfaces. It is similar to a class, but it does not have any of its methods implemented. Instead, each method’s body is empty, and only the method signatures are present. It could be defined using the interface keyword, followed by the interface name and declare method signatures without providing the implementation.
Let’s consider a scenario where you are developing a custom Salesforce application for a university. In this application, you need to create a class that represents a student. The Student class should implement two interfaces: Enrollable and ScholarshipEligible.
First, let’s define the two interfaces:
void enroll();
}
public interface ScholarshipEligible { Boolean isEligibleForScholarship();
}
// Student Class Implementing Interfaces
public class Student implements Enrollable, ScholarshipEligible { private String name;
private Boolean enrolled;
private Boolean hasGoodGrades;
public Student(String name, Boolean hasGoodGrades) { this.name = name;
this.hasGoodGrades = hasGoodGrades;
this.enrolled = false;
}
public void enroll() {
enrolled = true;
System.debug(name + ‘ has been enrolled.’);
}
public Boolean isEligibleForScholarship() { return hasGoodGrades;
}
}
Student student2 = new Student(‘Jane Smith’, false); student2.enroll(); // Output: Jane Smith has been enrolled. Boolean isEligible2 = student2.isEligibleForScholarship(); System.debug(‘Is eligible for scholarship: ‘ + isEligible2); // Output: Is eligible for scholarship: false
In this code block, the interface definitions, the Student class, and the usage example are combined. You can execute this code as an anonymous Apex block in the Developer Console. This code will create Student objects, enroll them, and check their scholarship eligibility. The debug logs will display the output messages.
By implementing multiple interfaces, you can define different sets of behaviors for your classes and achieve more flexible and modular code.
Interface methods are implicitly public and cannot be modified. Implementing classes must adhere to this visibility. Both classes and interfaces are fundamental to building well-organized, modular, and maintainable applications on the Salesforce platform.
Apex includes a variety of method-related keywords such as static, virtual, global, abstract, and instance which empower developers to create efficient, modular, and extensible code:
Static Methods
–– A static method belongs to the class itself, not to instances of the class.
–– It can be called using the class name, without creating an instance of the class.
–– Static methods cannot access instance variables, but they can access other static variables or other static methods.
Let’s say you have a class called StringUtil that contains a static method called capitalizeFirstLetter. This method takes a string as input and returns the string with the first letter capitalized. Here’s how the code would look:
Step 1: Create the class
public class StringUtil {
public static String capitalizeFirstLetter(String input) { if (String.isBlank(input)) {
return input;
}
return input.substring(0, 1).toUpperCase() + input.substring(1);
}
}
Step 2: Save the class
Step 3: Open the anonymous window
Step 4: Test the public class
String capitalizedString = StringUtil.capitalizeFirstLetter(inputString); System.debug(‘Original String: ‘ + inputString); System.debug(‘Capitalized String: ‘ + capitalizedString);
To use this static method, you can call it directly on the class name, without creating an instance of the StringUtil class. The provided Apex code above capitalizes the first letter of a given string using a static method in the StringUtil class.
This is just one example of how you can use Apex static methods. They are flexible and can be used in various scenarios to perform specific tasks without the need to create an instance of the class.