In this Salesforce tutorial, you will Understand about the class, abstract, interface, implements and extends keywords in Apex is a key part of creating solid Salesforce application. These OOP concepts allow developers to develop modular, reusable and maintainable code structures. You can use inheritance (extends), contracts (interface), multiple behaviors(implements) and abstraction(abstract) to design flexible systems which makes sense because they are easier to test, simpler to extend and cheaper in the long run.
Apex Reserved keywords
Certainly! Below is a comprehensive table of Apex Reserved Keywords. Reserved keywords are predefined words in the Apex programming language that have special meanings and cannot be used as identifiers (such as variable names, class names, or method names) in your code. Understanding these keywords is essential to avoid syntax errors and to write effective Apex code.
Apex Reserved Keywords
Keyword | Description |
---|---|
abstract | Declares an abstract class or method that must be implemented by subclasses. |
and | Logical AND operator used in conditional statements. |
as | Used for casting or type conversion. |
assert | Used for debugging to test assumptions in the code. |
break | Exits a loop or a switch statement prematurely. |
byte | Data type for 8-bit integers. |
case | Part of a switch statement to define a specific case. |
catch | Handles exceptions in try-catch blocks. |
class | Defines a new class. |
const | Declares a constant variable whose value cannot be changed after initialization. |
continue | Skips the current iteration of a loop and continues with the next one. |
currency | Data type for currency values, ensuring proper formatting and precision. |
default | Specifies the default case in a switch statement or the default access modifier. |
do | Used in do-while loops to execute a block of code at least once before checking the condition. |
else | Specifies an alternative block of code in conditional statements. |
enum | Defines an enumeration, a distinct data type consisting of a set of named constants. |
extends | Indicates that a class is inheriting from a superclass. |
false | Boolean literal representing the false value. |
final | Declares a class or method that cannot be subclassed or overridden. |
finally | Defines a block of code that executes after a try-catch , regardless of whether an exception was thrown. |
float | Data type for 32-bit floating-point numbers. |
for | Used to define a for loop for iterating over a range or collection. |
from | Used in SOQL queries to specify the data source. |
global | Access modifier indicating global visibility across namespaces. |
goto | Reserved for potential future use; not currently implemented in Apex. |
if | Initiates a conditional statement to execute code based on boolean expressions. |
implements | Indicates that a class implements an interface, thereby agreeing to perform the behaviors defined by the interface. |
import | Imports classes or packages to make their members available without using their fully qualified names. |
in | Used in for loops to iterate over collections. |
instanceof | Checks if an object is an instance of a specific class or interface. |
int | Data type for 32-bit integers. |
interface | Defines an interface, which is a contract that classes can implement. |
is | Used in type checking expressions (e.g., is , is not ). |
long | Data type for 64-bit integers. |
new | Creates a new instance of a class. |
null | Represents a null value, indicating the absence of a value. |
of | Used in specific contexts, such as with certain SOQL clauses. |
or | Logical OR operator used in conditional statements. |
override | Indicates that a method is overriding a method in its superclass. |
package | Access modifier indicating package-level visibility. |
private | Access modifier indicating private visibility within the class. |
protected | Access modifier indicating protected visibility, accessible within the class and its subclasses. |
public | Access modifier indicating public visibility, accessible from any other class. |
return | Exits a method and optionally returns a value. |
sObject | Represents a generic Salesforce object, the base class for all objects in Salesforce. |
short | Data type for 16-bit integers. |
static | Indicates that a member (variable or method) belongs to the class itself rather than to instances of the class. |
super | References the superclass, allowing access to its methods and constructors. |
switch | Defines a switch statement for multi-branch conditional execution. |
this | References the current instance of the class. |
throw | Throws an exception, signaling that an error has occurred. |
true | Boolean literal representing the true value. |
try | Defines a block of code to test for exceptions. |
void | Indicates that a method does not return a value. |
while | Used to define a while loop that continues as long as a condition is true. |
with | Used in class declarations to specify sharing rules (e.g., with sharing , without sharing ). |
Notes:
- Case Sensitivity:
- Apex is case-insensitive, meaning that keywords like
Class
,CLASS
, andclass
are treated the same. However, it’s a best practice to use lowercase for consistency and readability.
- Reserved vs. Non-Reserved Words:
- While all reserved keywords cannot be used as identifiers, non-reserved words may be used as identifiers but are discouraged to avoid confusion and potential conflicts with future language updates.
- Future Reserved Keywords:
- Some keywords are reserved for potential future use. It’s advisable to avoid using these as identifiers to ensure forward compatibility of your code.
- Contextual Usage:
- Certain keywords have specific contexts in which they are used. For example,
from
andof
are typically used within SOQL queries, whilewith
is used in class declarations to enforce sharing rules.
Best Practices:
- Avoid Using Reserved Keywords as Identifiers:
- Even though some reserved keywords might technically be allowed as identifiers, it’s highly recommended to avoid doing so to prevent confusion and maintain code clarity.
- Consistent Naming Conventions:
- Adopt a consistent naming convention (such as camelCase for variables and PascalCase for classes) to enhance code readability and maintainability.
- Stay Updated with Apex Documentation:
- Salesforce may introduce new reserved keywords in future releases. Regularly consulting the official Apex documentation ensures you stay informed about any changes.
1. Class
This keyword is used to define a class.
Example
private class MyClass { private Integer number; public Integer getNumber() { return number; } }
2. Abstract
This keyword is used to define abstract classes. An abstract class that contains methods only have a signature and no body is defined. Can also define methods.
Example
public abstract class MyAbstrtactClass { protected void myMethod1() { /* instruction */ } abstract Integer myAbstractMethod1(); }
3. Implements
This keyword is used to declare a class that impediments an interface.
Example
global class CreateTaskEmailExample implements Messaging.InboundEmailHandler { global Messaging.InboundEmailResulthandleInboundEmail(Messaging.inboundEmail email,Messaging.InboundEnvelope env) { // do some work, return value; } }
4. extends
Defines a class that extends another class.
Example
public class MyClass1 extends MyClass { // code }
5. interface
This keyword is used to define a data type with method signatures. Classes implement interfaces. An interface can extend another interface.
Example
public interface PO { public void doWork(); } public class MyPO implements PO { public override doWork() { // actual implementation } }
6. virtual
This keyword Defines a class or method that allows extension and overrides. You can’t override a method with the override keyword unless the class or method has been defined as virtual.
Example
public virtual class MyException extends Exception { // Exception class member // variable public Double d; // Exception class constructor MyException(Double d) { this.d = d; } // Exception class method protected void doIt() { } }