Tuesday, June 27, 2017

Salesforce Basics - Classes, Interfaces and Properties

Apex is an object-oriented programming language and this tutorial examines its support for these all important objects or class instances as they’re sometimes called. 

Objects are created from classes—data structures that contains class methods, instance methods, and data variables.
Classes, in turn, can implement an interface, which is simply a set of methods.

Classes and Objects: Classes are templates from which you can create objects.
Private Modifiers: The private modifier restricts access to a class, or a class method or member variable contained in a class, so that they aren’t available to other classes.
Static Variables, Constants and Methods: Static variables, constants, and methods don’t depend on an instance of a class and can be accessed without creating an object form of a class.
Interfaces: Interfaces are named sets of method signatures that don’t contain any implementation.
Properties: Properties allow controlled read and write access to class member variables.

Defining Classes
Apex classes are similar to Java classes. A class is a template or blueprint from which objects are created. An object is an instance of a class. For example, a Fridge class describes the state of a fridge and everything you can do with it. An instance of the Fridge class is a specific refrigerator that can be purchased or sold.

An Apex class can contain variables and methods. Variables are used to specify the state of an object, such as the object's name or type.
Since these variables are associated with a class and are members of it, they are referred to as member variables. Methods are used to control behavior, such as purchasing or selling an item.

Methods can also contain local variables that are declared inside the method and used only by the method. Whereas class member variables define the attributes of an object, such as name or height, local variables in methods are used only by the method and don’t describe the class.

Creating a new class
//Below is a new class called Fridge. The class has two member variables, modelNumber and
// numberInStock, and one method, updateStock. The void type indicates that the updateStock
// method doesn’t return a value.

public class Fridge {

    public String modelNumber;
    public Integer numberInStock;
 
    public void updateStock(Integer justSold) {
           numberInStock = numberInStock - justSold;
    }
}

//You can now declare variables of this new class type Fridge, and manipulate them. Execute the
// following in the Developer Console:

Fridge myFridge = new Fridge();
myFridge.modelNumber = 'MX-O';
myFridge.numberInStock = 100;
myFridge.updateStock(20);
Fridge myOtherFridge = new Fridge();
myOtherFridge.modelNumber = 'MX-Y';
myOtherFridge.numberInStock = 50;
System.debug('myFridge.numberInStock=' + myFridge.numberInStock);
System.debug('myOtherFridge.numberInStock=' + myOtherFridge.numberInStock);

Private Modifiers
The class, class methods, and member variables were all declared using the public keyword until now. This is an access modifier that ensures other Apex classes also have access to the class, methods, and variables. Sometimes, you might want to hide access for other Apex classes. This is when you declare the class, method, or member variable with the private access modifier.

By declaring the member variables as private, you have control over which member variables can be read or written, and how they’re manipulated by other classes. You can provide public methods to get and set the values of these private variables. These getter and setter methods are called properties and are covered in more detail in Property Syntax. Declare methods as private when these methods are only to be called within the defining class and are helper methods. Helper methods don’t represent the behavior of the class but are there to serve some utility purposes.

Note: By default, a method or variable is private and is visible only to the Apex code within the defining class. You must explicitly specify a method or variable as public in order for it to be available to other classes.

Let’s modify our Fridge class to use private modifiers for the member variables.

Constructors
Apex provides a default constructor for each class you create.

For example, you were able to create an instance of the Fridge class by calling new Fridge(), even though you didn’t define the Fridge constructor yourself. However, the Fridge instance in this case has all its member variables set to null because all uninitialized variables in Apex are null.
Sometimes you might want to provide specific initial values, for example, number in stock should be 0 and the model number should be a generic number. This is when you’ll want to write your own constructor. Also, it’s often useful to have a constructor that takes parameters so you can initialize the member variables from the passed in argument values.

Try adding two constructors, one without parameters and one with parameters.
1. Add the following to your Fridge class:

public Fridge() {
modelNumber = 'XX-XX';
numberInStock = 0;
}

public Fridge(String theModelNumber, Integer theNumberInStock) {
modelNumber = theModelNumber;
numberInStock = theNumberInStock;
}

The constructor looks like a method, except it has the same name as the class itself, and no return value.

2. You can now create an instance and set the default values all at once using the second constructor you’ve added. Execute the following:

Fridge myFridge = new Fridge('MX-EO', 100);
System.debug (myFridge.getModelNumber());

This will output 'MX-EO'. You'll often see classes with a variety of constructors that aid object creation.

Static Variables, Constants, and Methods
The variables and methods you've created so far are instance methods and variables, which means you have to first create an instance of the class to use the modelNumber and numberInStock variables. Each individual instance has its own copy of instance variables, and the instance methods can access these variables.

There are times when you need to have a member variable whose value is available to all instances, for example, a stock threshold variable whose value is shared with all instances of the Fridge class, and any update made by one instance will be visible to all other instances. This is when you need to create a static variable.

Static variables are associated with the class and not the instance and you can access them without instantiating the class.

You can also define static methods which are associated with the class, not the instance. Typically, utility methods that don’t depend on the state of an instance are good candidates for being static.

1. Modify the Fridge class by adding the following static variable:

public static Integer stockThreshold = 5;

2. Execute the following in the Developer Console:

System.debug ( Fridge.stockThreshold );

This will output 5. Note how you didn't have to create an instance of the Fridge class using the new operator. You just accessed the variable on the class.

3. You can also change the value of this static variable by accessing it through the class name.

// Modify the static stock threshold value
Fridge.stockThreshold = 4;
System.debug ( Fridge.stockThreshold );

This will write 4 to the output.

4. Sometimes you want to declare a variable as being a constant—something that won't change. You can use the final keyword to do this in Apex; it indicates that the variable might be assigned to a value no more than once. Modify the static variable you just declared to as follows:

public static final Integer STOCK_THRESHOLD = 5;

You can still output the value of the field, for example, Fridge.STOCK_THRESHOLD; will work, but you can now not assign any other value to the field, for example, Fridge.STOCK_THRESHOLD = 3; won't work.

5. Let's define a static class method that prints out the values of a given object that gets passed in as an argument. This will be a great help for debugging. Add a new method to the Fridge class:

public static void toDebug(Fridge aFridge) {

System.debug ('ModelNumber = ' + aFridge.modelNumber);
System.debug ('Number in Stock = ' + aFridge.numberInStock);
}

6. Test out this new method by calling it in the Developer Console and passing in a Fridge instance:

Fridge myFridge = new Fridge('MX-Y', 200);
Fridge.toDebug(myFridge);

This is the output you’ll get in the Developer Console.

You now have an easy way to dump any object you create to the Developer Console!

ID & Blob Datatypes
ID
The ID data type represents an 18-character an object identifier. Force.com sets an ID to a object once it is inserted into the database.
For example, an ID value can be ‘a02D0000006YLCyIAO’.

Blob
The Blob data type represents binary data stored as a single object. Examples of Blob data is attachments to email messages or the body of a document. Blobs can be accepted as Web service arguments. You can convert a Blob data type to String or from String using the toString and valueOf methods, respectively. The Blob data type is used as the argument type of the Crypto class methods.

1 comment:

Lightning Inter-Component Communication Patterns

Lightning Inter-Component Communication Patterns If you’re comfortable with how a Lightning Component works and want to build producti...