In This post we will learn about Apex Classes ,Variable,Interface And Properties.
Apex Classes
One of the benefits of Apex classes is code reuse. Class methods can be called by triggers and other classes.
Apex is an object-oriented programming. Objects are created from classes-data structures that contain 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 from of a class.
- Interfaces: Interfaces are named sets of methods 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 that 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 behaviour, 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 and Instantiating Classes
public class Fridge { public String modelNumber; public Integer numberInStock; public void updateStock(Integer justSold) { numberInStock = numberInStock – justSold; } }
The class has two member variables, modelNumberand numberInStock, and one method, update stock. The voidtype indicates that the updateStock method doesn’t return a value.
You can now create Object of this new class type Fridge, and manipulate them.
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
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.
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 behaviour of the class but are there to serve some utility purposes.
By default, a method or variable is private and is visible only to the Apex code within the defining class.
Let’s modify our Fridgeclass to use privatemodifiers for the member variables and include getter and setter methods.
Constructors
Apex provides a default constructor for each class you create. For example, you were able to create an instance of the Fridgeclass by calling new Fridge(), even though you didn’t define the Fridgeconstructor yourself.
However, the Fridgeinstance in this case has all its member variables set to nullbecause all uninitialized variables in Apex are null.
Sometimes you might want to provide specific initial values, 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.
publicFridge() { modelNumber = ‘XX-XX’; numberInStock = 0; } Public Fridge(String theModelNumber, Integer the NumberInStock) { modelNumber = theModelNumber; numberInStock = theNumberInStock; }
You can now create an instance and set the default values all at once using the second constructor.
Fridge myFridge = new Fridge(‘MX-EO’, 100);
System.debug(myFridge.getModelNumber());
Static Variables, Constants, and Methods
Each individual instance has its own copy of instance variables, and the instance methods can access these variables. 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.
Public static Integer stockThreshold = 5;
To access this:
System.debug (Fridge.stockThreshold );
To declare a variable as being a constant- something that won’t change. You can use the finalkeyword to do this in Apex;
public static final Integer STOCK_THRESHOLD = 5;
For static method we use:
public static void toDebug(Fridge aFridge) {
System.debug (‘ModelNumber = ‘+aFridge.modelNumber);
System.debug (‘Nimber in Stock = ‘+aFridge.numberInStock);
}
Interfaces
An interface is a named set of method signatures (the return and parameter definitions), but without any implementation. Interfaces provide a layer of abstraction to your code. They separate the specific implementation of a method from the declaration for that method.
This way, you can have different implementations of a method based on your specific application.
For example, a fridge is a type of kitchen appliance, and so is a toaster. Since every kitchen appliance has a model number, the corresponding interface can have a getModelNumberMethod. However, the format of the model number is different for different appliances. The Fridge class and the toaster class can implement this method such that they return different formats for the model number. Create an interface in the same way that you create a class:
public interface KitchenUtility {
String getModelNumber();
}
implement this interface.
public class Fridge implements KitchenUtility {
//put your code here
}
public class Toaster implements KitchenUtility {
private String modelNumber;
public String getModelNumber() {
return ‘T’ + modelNumber;
}
}
To access the code:
Fridge f = new Fridge(‘MX’, 200);
Toaster t = new Toaster();
KitchenUtility[] utilities = new
KitchenUtility[] { f, t };
String model =
Utilities [0].getModelNumber();
System.debug(model);
Property Syntax
In Private Modifiers, you modified the variables to be private, ensuring that they can only be accessed through a method and there is a shorthand syntax that lets you define a variable and code that should run when the variable is accessed or retrieved.
Add a new property, ecoRating, to the Fridgeclass by adding the following:
public Integer ecoRating
{
get { return ecoRating; }
set { ecoRating = value; if (ecoRating <0) ecoRating =0;
}
}
Hits: 1485