Private class – Classes, Interfaces, and Methods – Data Manipulation Language (DML) – Salesforce Certified Platform Developer I Study Guide

Private class is a class that can only be accessed within the same Apex class or trigger in which it is defined. It cannot be accessed outside of the class or trigger, including within other classes or triggers in the same Salesforce organization. Private classes are primarily used for encapsulating functionality that is specific to a particular class or trigger, and are not intended to be used externally. Access modifiers can be also used to control the visibility and accessibility of variables and methods in Apex only within the class where they are declared. Here is an example for private access modifier within the classPrivate classes that are commonly used in Apex for the following purposes:

•\ Helper Classes: Private classes can be used as helper classes within a larger class or trigger to encapsulate complex or reusable logic that is specific to that class or trigger. This helps in organizing and modularizing the code, making it easier to read and maintain.

•\ Inner Classes: Private classes can be defined as inner classes within another class. Inner classes have access to the variables and methods of the outer class, allowing for tighter integration and sharing of data.

•\ Data Structures: Private classes can be used to define custom data structures that are used internally within a class or trigger. These data structures can hold related data and provide methods for manipulating and accessing that data.

Here’s a simple example:

Step 1: Create the private class

public class MyOuterClass {

private class MyPrivateClass {

public void myMethod() {

System.debug(‘Hello PrivateClass’);

}

}

private Integer count = 2;

public void callPrivateMethod() { MyPrivateClass obj = new MyPrivateClass(); obj.myMethod();

displayInfo();

}

public void displayInfo() {

System.debug(count);

}

public Integer getCount() {

return count;

}

}

Step 2: Save the class

Step 3: Open the anonymous window

Step 4: Test the public class

MyOuterClass outerInstance = new MyOuterClass(); outerInstance.callPrivateMethod();

Integer countValue = outerInstance.getCount(); System.debug(‘Count from getCount method: ‘ + countValue);

In this code block MyOuterClass now includes both the private inner class MyPrivateClass and the private variable count. The callPrivateMethod method demonstrates calling the private method myMethod from MyPrivateClass, and it also calls the displayInfo method to show the value of the private variable count. Finally, it calls the getCount method to demonstrate accessing the private variable directly.

Extend keyword in Apex is used to define a class that extends another class. When a class extends another class, it inherits all the properties and methods from the parent class, allowing you to reuse code and add additional functionality specific to the child class.

Step 1: Create the ParentClass

public virtual class ParentClass {

public void displayMessage() { System.debug(‘Hello from ParentClass’);

}

}

Step 2: Save the ParentClass

Step 3: Create the ChildClass

public class ChildClass extends ParentClass { public void displayChildMessage() {

System.debug(‘Hello from ChildClass’);

}

}

Step 4: Save the ChildClass

Step 5: Open the anonymous window

Step 6: Test the ParentClass and ChildClass

  • Example usage in the Anonymous Apex window ChildClass childInstance = new ChildClass(); childInstance.displayMessage();
  • This will call the method from ParentClass childInstance.displayChildMessage();
  • This will call the method from ChildClass

In this example, ChildClass extends ParentClass. This means that ChildClass inherits the displayMessage method from ParentClass. Additionally, ChildClass has its own method displayChildMessage. When you create an instance of ChildClass and call its methods, it will execute both the inherited method from the parent class and its own method.

Apex also supports inner classes, which are classes defined within another class. Inner classes have access to the properties and methods of the outer class. You can create an instance of an inner class within the outer class or outside the outer class, similar to any other class.

In the below example, the OuterClass class contains an inner class called InnerClass.

Step 1: Create the class

public class OuterClass {

public class InnerClass {

public void displayMessage() { System.debug(‘Hello from InnerClass’);

}

}

}

Step 2: Save the class

Step 3: Open the anonymous window

Step 4: Test the outer class

  • Example usage in the Anonymous Apex window OuterClass outerInstance = new OuterClass(); OuterClass.InnerClass innerInstance = new OuterClass.InnerClass();

innerInstance.displayMessage();