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

KeywordDescription
abstractDeclares an abstract class or method that must be implemented by subclasses.
andLogical AND operator used in conditional statements.
asUsed for casting or type conversion.
assertUsed for debugging to test assumptions in the code.
breakExits a loop or a switch statement prematurely.
byteData type for 8-bit integers.
casePart of a switch statement to define a specific case.
catchHandles exceptions in try-catch blocks.
classDefines a new class.
constDeclares a constant variable whose value cannot be changed after initialization.
continueSkips the current iteration of a loop and continues with the next one.
currencyData type for currency values, ensuring proper formatting and precision.
defaultSpecifies the default case in a switch statement or the default access modifier.
doUsed in do-while loops to execute a block of code at least once before checking the condition.
elseSpecifies an alternative block of code in conditional statements.
enumDefines an enumeration, a distinct data type consisting of a set of named constants.
extendsIndicates that a class is inheriting from a superclass.
falseBoolean literal representing the false value.
finalDeclares a class or method that cannot be subclassed or overridden.
finallyDefines a block of code that executes after a try-catch, regardless of whether an exception was thrown.
floatData type for 32-bit floating-point numbers.
forUsed to define a for loop for iterating over a range or collection.
fromUsed in SOQL queries to specify the data source.
globalAccess modifier indicating global visibility across namespaces.
gotoReserved for potential future use; not currently implemented in Apex.
ifInitiates a conditional statement to execute code based on boolean expressions.
implementsIndicates that a class implements an interface, thereby agreeing to perform the behaviors defined by the interface.
importImports classes or packages to make their members available without using their fully qualified names.
inUsed in for loops to iterate over collections.
instanceofChecks if an object is an instance of a specific class or interface.
intData type for 32-bit integers.
interfaceDefines an interface, which is a contract that classes can implement.
isUsed in type checking expressions (e.g., is, is not).
longData type for 64-bit integers.
newCreates a new instance of a class.
nullRepresents a null value, indicating the absence of a value.
ofUsed in specific contexts, such as with certain SOQL clauses.
orLogical OR operator used in conditional statements.
overrideIndicates that a method is overriding a method in its superclass.
packageAccess modifier indicating package-level visibility.
privateAccess modifier indicating private visibility within the class.
protectedAccess modifier indicating protected visibility, accessible within the class and its subclasses.
publicAccess modifier indicating public visibility, accessible from any other class.
returnExits a method and optionally returns a value.
sObjectRepresents a generic Salesforce object, the base class for all objects in Salesforce.
shortData type for 16-bit integers.
staticIndicates that a member (variable or method) belongs to the class itself rather than to instances of the class.
superReferences the superclass, allowing access to its methods and constructors.
switchDefines a switch statement for multi-branch conditional execution.
thisReferences the current instance of the class.
throwThrows an exception, signaling that an error has occurred.
trueBoolean literal representing the true value.
tryDefines a block of code to test for exceptions.
voidIndicates that a method does not return a value.
whileUsed to define a while loop that continues as long as a condition is true.
withUsed in class declarations to specify sharing rules (e.g., with sharing, without sharing).

Notes:

  1. Case Sensitivity:
  • Apex is case-insensitive, meaning that keywords like Class, CLASS, and class are treated the same. However, it’s a best practice to use lowercase for consistency and readability.
  1. 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.
  1. 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.
  1. Contextual Usage:
  • Certain keywords have specific contexts in which they are used. For example, from and of are typically used within SOQL queries, while with 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() {
	
	}
}