A class is a fundamental component that defines an object’s functionality and characteristics. The basic structure of an Apex class consists of class variables also known as properties, and methods defined within the class.
It serves as a template or blueprint from which objects are created and defines the behavior and properties. Properties are variables defined within a class that store data. An object is an instance of a class. Each instance of the class has its own set of values for these variables. To create an object of a class in Apex, you use the ”new” keyword followed by the name of the class.
Methods within the class define actions that objects can perform. The method signature consists of several components such as name, return type, and parameter list that define the method and its behavior. In Apex, method signatures must be unique within the same class. This means that you cannot have two methods in the same class with the same name and the same parameter list.
Overloading is allowed in Apex, which means you can have multiple methods with the same name but with different parameter lists within the same class. The compiler differentiates between these methods based on the number or types of parameters. Here’s an example of method overloading:
public class MyClass {
public Integer addNumbers(Integer a, Integer b) { return a + b;
}
public Double addNumbers(Double a, Double b) { return a + b;
}
}
In this example, there are two addNumbers methods in the same class. One takes two Integer parameters, and the other takes two Double parameters. They have different method signatures because of the difference in parameter types.
A class can have an optional constructor that is used for initializing objects. When an object is created, the constructor is called to set the initial values of the object’s variables.
In the provided code example here, the MyFruit class has two class variables, fruit and count. The class also has an optional constructor. The method displayInfo is a void method which doesn’t return any value. The purpose of the displayInfo method is to output the values of the fruit and count variables to the debug log.
When an object of the MyFruit class is created using the “new” keyword, the displayInfo method can be called to display the values of the fruit and count variables.
public class MyFruit {
Integer count;
MyFruit() {
fruit = ‘orange’;
count = 1;
}
System.debug(fruit);
System.debug(count);
}
}
Here’s another example that demonstrates the usage of constructors in Apex:
public class Fruit {
public String name;
public Integer quantity;
name = ‘Unknown’; quantity = 0;
}
public Fruit(String name, Integer quantity) { this.name = name;
this.quantity = quantity;
}
System.debug(‘Fruit Information:’); System.debug(‘Name: ‘ + name); System.debug(‘Quantity: ‘ + quantity);
}
return quantity > 100;
}
}
//You can use this Fruit class by creating instances of it, setting properties, and calling its methods.
Fruit myFruit = new Fruit(‘Apple’, 150); myFruit.displayInfo();
System.debug(‘Is it in abundance? ‘ + myFruit.isInAbundance());
This example demonstrates how to use the new Fruit class with the adjusted properties and methods.
Overall, the basic process of creating an object of a class with an optional constructor involves calling the constructor to initialize the object’s variables and then calling
any methods defined within the class on the object. Here are some key points about constructors in Apex:
•\ A constructor has the same name as the class it belongs to.
•\ The access specifier for a constructor is typically public.
•\ Constructors are invoked automatically when an object is created
using the new keyword.
•\ Constructors can be used to set default or required values for an
object when it is first created.
•\ If a class does not have a user-defined constructor, a default, no-
argument, public constructor is provided by the compiler.
•\ Constructors can be overloaded, meaning a class can have multiple
constructors with different parameter lists.
•\ Constructors can perform initialization tasks, such as assigning
values to instance variables or calling other methods.
The next important aspect of classes is understanding access modifiers. In Apex, access modifiers determine the visibility and accessibility of classes, methods, and variables in your code. 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. There are three main access modifiers for classes in Apex: global, public, and private.