Classes Objects and Interfaces

Class Definition

Java

Class or interface in Java type consists of an identifier or a dotted sequence of identifiers; each identifier is optionally followed by type arguments. If type arguments appear anywhere in a class or interface type, it is a parameterized type also known as generic types.

Each identifier in a class type is classified as a package name or a type name

public class Box {
        private Object object;

        public void set(Object object) { this.object = object; }
        public Object get() { return object; }
    }

Apex

Apex supports classes. An Apex class is a template from which objects can be created. An object is an instance of an apex class. For example, the Invoice class describes an invoice, and all the operations possible on the invoice An instance of the Invoice class is a specific invoice that you send or receive.

All objects have state and behavior, things which an object knows about itself, and things that an object can do.

Class can contain variables and methods. Variables specify the state of an object, such as the object’s Name or Type. These variables are associated with a class and are members of it; they are commonly referred to as member variables. Methods are used to control behavior of a class Class can contain other classes, exception types, and initialization code.

You have to use public or global followed by class key word and the Class name.

public class Box {

}

global class Box {

}

Example Class in Apex with member variables

public class Point {
    Integer x = 1, y = 1;
    void move(Integer dx, Integer dy) {
        x += dx;
        y += dy;
        alert();
    }
    void alert(){
        System.debug('x:' + x + ', ' + 'y: ' + y);
    }
}

Generic Defintiion of a Class

Java

private | public
[abstract]
class ClassName [implements InterfaceNameList] [extends ClassName]
{
        // The body of the class
}

Apex

Notice that Apex has some additional qualifiers in addition to the generic Java qualifiers

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

Parameterized Class

Java

Supported in Java

public class Box<T> {
    // T stands for "Type"
    private T t;

    public void set(T t) { this.t = t; }
    public T get() { return t; }
}

Apex

Not supported in Apex

Enum Class

Enum class in Java

public enum Day {
    SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
    THURSDAY, FRIDAY, SATURDAY
}

Enum class in Apex

Apex supports Enum in the same way as Java

public enum Day {
    SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
    THURSDAY, FRIDAY, SATURDAY
}

Day d = Day.SUNDAY;
System.debug(d);

PACKAGES

Packages in Java

package com.storage;

public class Box {

}

Packages in Apex

Packages are not supported in Aprex

Fields

Java Fields

Member variables in a class—these are called fields. Variables in a method or block of code—these are called local variables. Variables in method declarations—these are called parameters.

Field declarations are composed of three components, in order:

  • Zero or more modifiers, such as public or private.
  • The field’s type.
  • The field’s name.
[public | private | protected ] [final] [static]
        data_type variable_name [= value]

Example

private static final int MY_INT;
private final int i = 1;

Apex Fields

Apex fields are very similar to Java fields.

  • Optional: Modifiers, public or final, static.
  • Required: The data type of the variable, e.g String, Boolean, Integer
  • Required: The name of the variable.
  • Optional: The value of the variable.
[public | private | protected | global] [final] [static]
        data_type variable_name [= value]

Example

private static final Integer MY_INT;
private final Integer i = 1;