Saturday, December 19, 2015

Apex Class Defination

A class is a template or blue print from which objects are created. A class can contain variables and methods. Variables are used to specify the state of an object and Methods are used to control behaviour. A class can contain other classes, exception type and initialization code.
An Interface is a class which none of the methods have been implemented- the method signatures are there, but the body of each method is empty.
public class outerClass {
    // Outer Class Code
    class innerClass {
       //Inner Class Code
    }
}
# Access modifier for outer class should be public or global.
# You do not have to use an access modifier in the declaration of an inner class.
Use the Syntax for class defination -

private | public | global | [virtual | abstract | with sharing | without sharing] class ClassName [Implements InterfaceNameList] [extends ClassName]
{
      //The body of the class
}

# Private access modifier declares that this class is only known locally, that is, only by this section of code. This is the default access for inner classes—that is, if you don't specify an access modifier for an inner class, it is considered private. This keyword
can only be used with inner classes.
# Public access modifier declares that this class is visible in your application or namespace.
# The global access modifier declares that this class is known by all Apex code everywhere. All classes that contain methods defined with the webService keyword must be declared as global. If a method or inner class is declared as global, the outer,
top-level class must also be defined as global.
# The with sharing and without sharing keywords specify the sharing mode for this class.
# The virtual definition modifier declares that this class allows extension and overrides. You cannot override a method with the override keyword unless the class has been defined as virtual.
The abstract definition modifier declares that this class contains abstract methods, that is, methods that only have their signature declared and no body defined.
A class can implement multiple interfaces, but only extend one existing class. This restriction means that Apex does not support multiple inheritance. The interface names in the list are separated by commas.

Class Variables -
To declare a variable, specify the following:
• Optional:  Modifiers, such as public or final, as well as static.
• Required: The data type of the variable, such as String or Boolean.
• Required: The name of the variable.

• Optional:  The value of the variable.
Syntax - 
[public | private | protected | global] [final] [static] data_type variable_name
[= value]

Class Methods-
To define a method, specify the following:
• Optional: Modifiers, such as public or protected.
• Required: The data type of the value returned by the method, such as String or Integer. Use void if the method does not return a value.
• Required: A list of input parameters for the method, separated by commas, each preceded by its data type, and enclosed in parentheses(). If there are no parameters, use a set of empty parentheses. A method can only have 32 input parameters.
• Required: The body of the method, enclosed in braces {}. All the code for the method, including any local variable declarations, is contained here.
Syntax for defining a method:
[public | private | protected | global] [override] [static] data_type method_name (input parameters)
{
     // The body of the method
}
Note: You can only use override to override methods in classes that have been defined as virtual.

No comments:

Post a Comment