Apex Access Modifiers And Data Security Deep Dive

Introduction:

Access modifiers play a pivotal role in Apex programming, governing the visibility and accessibility of classes, methods, and variables within Salesforce development. In this blog, we’ll unravel the secrets behind various access modifiers and key concepts like Object Level Access, Record Level Access, and Organization-Wide Defaults (OWD), providing clear explanations and illustrative examples to empower you in writing secure and efficient Apex code.

1. Private :

Definition:

Private members are only accessible within the defining class.

Explanation:

It’s like having a secret room accessible only from within your house. Private members keep their functionality hidden from the outside world.

Example:
/*
Private access modifier restricts the visibility of a variable, method, or class to only the defining class.
Members declared as private are not accessible outside the class.
*/
public class MyClass {
private Integer myPrivateVariable;
private void myPrivateMethod() {
// Keep it private, shhh!
}
}
view raw Private hosted with ❤ by GitHub

2. Public:

Definition:

Public members are accessible from any class in the same namespace.

Explanation:

Think of it as hosting an open house party where anyone from the same neighborhood can join. Public members are open for everyone to access.

Example:
/*
Public access modifier makes a variable, method, or class accessible from
any other class or trigger in the same namespace or different namespace.
*/
public class MyClass {
public Integer myPublicVariable;
public void myPublicMethod() {
// Open to the public!
}
}
view raw Public hosted with ❤ by GitHub

3. Global:

Definition:

Global members can be accessed across namespaces and by code outside of the Salesforce org.

Explanation:

Going global means your class is like a celebrity, accessible not only to your Salesforce pals but to folks outside your organization. It has universal fame!

Example:
/*
Global access modifier makes a class or method available across
different namespaces, providing the highest level of visibility.
It is often used in the development of managed packages.
*/
global class MyGlobalClass {
global Integer myGlobalVariable;
global void myGlobalMethod() {
// Global reach!
}
}
view raw Global hosted with ❤ by GitHub

4. With Sharing:

Definition:

Enforces sharing rules of the current user.

Explanation:

With Sharing is like playing a game where everyone shares their toys according to the rules. It ensures data access follows the sharing settings and permissions of the current user.

Example:
/*
With Sharing is used in Apex classes to enforce sharing rules of the current user.
It ensures that the code runs with the sharing rules of the current user, limiting
access to records based on their sharing settings.
*/
with sharing class MySharingClass {
// Code logic with sharing rules!
}
view raw With Sharing hosted with ❤ by GitHub

5. Without Sharing:

Definition:

Ignores the sharing rules, allowing unrestricted access to data.

Explanation:

Without Sharing is like having your own playground where there are no rules. It provides unrestricted access to data without considering sharing settings.

Example:
/*
Without Sharing is used in Apex classes to ignore sharing rules.
When this modifier is used, the code runs in system mode and has
access to all records regardless of the user's sharing settings.
*/
without sharing class MyNoSharingClass {
// Code logic without sharing restrictions!
}
view raw Without Sharing hosted with ❤ by GitHub

6. Virtual:

Definition:

Allows extension of a class, enabling method override.

Explanation:

Virtual is like sending out invitations to a costume party. You can come dressed as yourself, or you can change it up in your own way! It allows a class to be extended, and its methods can be overridden in child classes.

Example:
/*
Virtual is used with methods to indicate that the method can be overridden by a subclass.
It allows for polymorphism, enabling a subclass to provide a specific implementation for the method.
*/
public virtual class MyBaseClass {
public virtual void myVirtualMethod() {
// Ready for a virtual adventure!
}
}
view raw Virtual hosted with ❤ by GitHub

7. Abstract:

Definition:

Similar to virtual, but cannot be instantiated directly.

Explanation:

Abstract classes are like blueprints for houses. You can’t live in a blueprint, but you can use it to build your dream home. Similarly, abstract classes cannot be instantiated on their own, but they provide a structure for other classes.

Example:
/*
Abstract is used with classes or methods to indicate that they must be implemented by any concrete subclass.
Abstract classes cannot be instantiated, and abstract methods do not have a body in the declaring class.
*/
public abstract class MyAbstractClass {
public abstract void myAbstractMethod();
}
view raw Abstract hosted with ❤ by GitHub

8. Static:

Definition:

Marks a method or variable as belonging to the class rather than an instance.

Explanation:

Static is like having a shared whiteboard. It belongs to the class, not to individual instances. Everyone can see and write on it, and changes are shared among all instances of the class.

Example:
/*
Static is used with variables and methods to make them belong to the class rather than an instance
of the class. Static members are shared among all instances of a class and can be accessed without
creating an instance of the class.
*/
public class MyStaticClass {
public static Integer myStaticVariable;
public static void myStaticMethod() {
// Code logic shared by all instances!
}
}
view raw Static hosted with ❤ by GitHub

9. Final:

Definition:

Prevents further extension of a class or overriding of a method.

Explanation:

Final is like saying “This is it, no more changes!” You can’t extend a final class or override a final method. It’s a way to lock down a class or method.

Example:
/*
Final is used with classes, methods, or variables to indicate that they cannot be further extended,
overridden, or modified. For classes, it means no subclassing; for methods, it means no overriding;
for variables, it means they cannot be reassigned.
*/
public final class MyFinalClass {
// This is the final stop!
}
view raw Final hosted with ❤ by GitHub

10. Interface:

Definition:

Defines a contract for a class to implement its methods.

Explanation:

Interfaces are like promises. If a class says it implements an interface, it’s making a commitment to have certain methods. It’s a way to ensure that classes follow a specific structure.

Example:
/*
Interface is used to define a contract for classes that implement it.
All methods declared in an interface must be implemented by any class that implements the interface.
*/
public interface MyInterface {
void myInterfaceMethod();
}
view raw Interface hosted with ❤ by GitHub

11. Override:

Definition:

Indicates that a method in a child class is intended to override a method in the parent class.

Explanation:

Override is like saying, “Hey, I want to use my version of this method!” It’s a way to customize methods inherited from a parent class.

Example:
/*
Override is used to indicate that a method in a subclass is intended to override a method with the
same signature in its superclass. This annotation helps catch errors at compile time if the method
is not actually an override.
*/
public class MyChildClass extends MyBaseClass {
public override void myVirtualMethod() {
// My own version of the virtual adventure!
}
}
view raw Override hosted with ❤ by GitHub

12. Constructor:

Definition:

Special method invoked when an object is instantiated.

Explanation:

Constructors are like the welcome party for your objects. They set things up when the objects are born. It’s the first method that gets called when you create an object.

Example:
/*
Constructors are special methods used to initialize objects when they are created.
In Apex, the constructor name is the same as the class name, and there can be
multiple constructors with different parameters.
*/
public class MyClass {
public MyClass() {
// Welcome to the object world!
}
}
view raw Constructor hosted with ❤ by GitHub

13. Super:

Definition:

Refers to the parent class in the inheritance hierarchy.

Explanation:

Super is like calling your superhero parent for help. It lets you access methods from the parent class. It’s handy when you want to use or extend functionality from the class you’re inheriting from.

Example:
/*
Super is used to call the constructor or method of the immediate superclass.
It is often used in the constructor of a subclass to invoke the constructor of its superclass.
*/
public class MyChildClass extends MyBaseClass {
public void myMethod() {
super.myVirtualMethod();
// Calling the superhero parent!
}
}
view raw Super hosted with ❤ by GitHub

14. Object Level Access (OLAs):

Definition:

Governs access to Salesforce objects like standard and custom objects.

Explanation:

Object Level Access determines who can see and interact with specific objects in your Salesforce organization.

Example:
/*
Object-level access in Salesforce refers to the permissions and visibility settings applied to
an entire object. This includes settings such as whether a user or profile has read, create, edit,
or delete access to records of a particular object. Object-level access is set using profiles and
permission sets.
*/
public class ObjectAccessExample {
Account acc = new Account(); // Accessing the Account object
// Code logic here
}

15. Record Level Access (RLAs):

Definition:

Controls access to individual records based on user permissions.

Explanation:

Record Level Access ensures that users can only view or modify records for which they have the appropriate permissions.

Example:
/*
Record-level access in Salesforce defines the level of access a user has to individual records
within an object. This can be controlled through mechanisms like sharing rules, manual sharing,
criteria-based sharing, and ownership. Record-level access ensures that users can only view or
modify records for which they have the appropriate permissions.
*/
public class RecordAccessExample {
List<Account> accList = [SELECT Id, Name FROM Account WHERE OwnerId = :UserInfo.getUserId()];
// Accessing only records owned by the current user
// Code logic here
}

16. Organization-Wide Defaults (OWD):

Definition:

Sets the default level of access to records for all users in the organization.

Explanation:

OWD defines the baseline access for records and ensures that even if there are no explicit sharing rules, there’s a default level of access.

Example:
/*
Organization-Wide Defaults (OWD) in Salesforce define the baseline level of access for records
across the entire organization. OWD settings control the default level of access for objects
and specify whether records are visible to all users, only those in the same role hierarchy,
or only the record owner. OWD settings can be set per object and are a fundamental aspect of
Salesforce's security model. They serve as the starting point for defining record access,
and additional sharing rules or manual sharing can be applied to further refine access.
*/
public class OWDAccessExample {
// Accessing records based on the organization-wide defaults
// Code logic here
}

Conclusion:

We’ve covered an array of access modifiers and introduced you to crucial concepts like Object Level Access, Record Level Access, and Organization-Wide Defaults. By combining your knowledge of access modifiers with data security practices, you’ll become a Salesforce developer who not only writes powerful code but also ensures data integrity and security. So, continue your learning journey, embrace these concepts, and happy coding! 🚀

Leave a comment