Apex Classes Simplified
What are Apex Classes or what is a Class itself?
The introduction of Class in the programming world was revolutionary which brought ease in the development and understanding of the code after development.
“A class is a template that describes the kinds of state and behaviors that object of its type support”. What does this mean?
Let’s understand this with an example of a Chef.
A Chef can have properties such as ‘Name’, ‘Age’, ‘Location’, ‘Specialization’. These are called attributes.
It can have methods/behavior such as cooking, tasting and training. These are called methods or behaviors a chef can perform.
You can use this Chef class to create different objects such as Pastry Chef, Sauce Chef, Vegetable Chef. These objects will have the same set of attributes that were defined in Chef Class. The value held in these attributes is referred to as state and it can differ from one object to other.
We will be creating the Chef Class as well but before that let’s see the basic block for class declaration.
Declaring Apex Class :
public| private | global [virtual | abstract | with sharing | without sharing | (none) ] class ClassName [implements InterfaceNameList | (none) ] [extends ClassName | (none)] {
}
Let’s split the above declaration into sub-segments to get a clearer view.
public | private | global
The above declaration implies that a class can be either public or global. These are called Access Modifiers. So what’s the big deal with the public, private or global?
public
If you declare a class as public then it will be visible to other classes in the same namespace. Any other class can call or refer to any public classes in the same namespace.
Now, what is namespace? You can consider namespace as a package or folder in which all your classes will be present. There is a default namespace present when you have not declared or used any other namespaces.
private
If you declare a class as private then it will not be visible to other classes. You can only declare Test Classes and inner classes as private. Any other top-level class can not be declared as private.
global
If you declare a class as global then it is globally available across all namespaces. Consider it as a global citizen who can travel freely across all the namespaces without any passport.
[virtual | abstract | with sharing | without sharing | (none) ]
Above are optional declarations that serve specific purposes. Let’s see them one by one :
virtual
If you declare a class as virtual it implies that this class can be extended and can be used as a parent class.
abstract
If you declare a class as abstract it implies that the class has abstract methods and the child class which is going to extend this abstract class has to implement the abstract methods.
In Apex, Inheritance is achieved using virtual or abstract. The concept of Inheritance is one of the pillars of Object-Oriented Programming. You can know more about Inheritance and its usage in Apex Classes over here – Apex Inheritance
with sharing
If you declare a class as with sharing it implies that the class should run in user mode rather than system mode.
What does this mode mean? A class by default runs in System Mode or we can say with the permissions of System Admin. Basically, all the records are accessible when a class is executed under system mode. If a class run in User mode then the access level is considered to be of that running user and records are returned depending upon the user permissions to records.
without sharing
If you declare a class as without sharing then the class will run in system mode. Didn’t we say above that by default class runs in system mode? So why do we have to explicitly declare to run in system mode using without sharing?
The reason is due to the fact that if a source class is running in User Mode then all the classes invoked from there also run in User Mode only. To allow another calling class to run in System mode we have to declare the class using without sharing.
[implements InterfaceNameList | (none) ]
The above declaration is for implementing Interface.
implements
A class can implement an Interface.
The interface is like a contract or agreement which a class has to follow. You can know more about Interface over here – Apex Interfaces
[extends ClassName | (none)]
The above block lets you extend a class from a parent class.
extends
If you have visited Apex Inheritance which we have shared above you will be knowing this by now. You can again revisit it for a quick recap 🙂
Access Modifiers for Class Members
Class members can be data members such as attributes and member functions which are the methods class has. We can determine a specific access level for these members which are as follows :
global: A member can be accessed across all classes within different namespaces.
public: A member can be accessed across all classes within the same namespace.
protected: A member can be accessed by inner classes or child classes within the same namespace.
private: A member can not be accessed from other classes. This is by default.
Please note that a class can not get more restrictive access than its members. For example – you have declared a class as public but trying to declare a method with global access modifier. Apex compiler will throw an error saying “Defining type for global methods must be declared as global”.
Now Let’s see our Chef Class which we discussed above in action :
public class Chef {
//Attributes
public String name;
public String specialization;
public String locaton;
public Integer age;
// Behaviours
public void cooking() {
System.debug('Chef is cooking awesome food.');
}
public void tasting() {
System.debug('Chef is doing quality check.');
}
public void training() {
System.debug('Chef is training the staff.');
}
}
In the above code block, we have created a public class Chef with members and methods with public access modifier for members. Thus, members can be called from any other classes within the same namespace.
Let us also discuss a key concept called constructors. Feel free to explore it as it is the one that is invoked for the first time when a class is used to create an object. Check it over here – Apex Constructors
You can also check How to create apex classes in Salesforce over here – Steps To Create Apex Classes
Now you can create your own Apex classes and play around to see how wonderful is Apex 🙂
Thank you for visiting SalesforceBlue.com
If you have any queries feel free to write down a comment below 🙂