Apex is an object-oriented language. This subsection describes Apex in terms of five standard characteristics of object-oriented languages, summarized here:
Encapsulation—Encapsulation combines the behavior and internal state of a program into a single logical unit.
Information hiding—To minimize tight coupling between units of a program, information hiding limits external visibility into the behavior and state of a unit.
Modularity—The goal of modularity is to establish clear boundaries between components of a program.
Inheritance—Inheritance allows one unit of code to define its behavior in terms of another.
Polymorphism—Polymorphism is the capability to interact with multiple units of code interchangeably without special cases for each.
These principles of object-oriented programming help you learn the Apex syntax and behaviors from a language-neutral point of reference.
Encapsulation describes the bundling of a program’s behavior and state into a single definition, usually aligned with some real-world concept. In Apex that definition is a class.
When a class is defined, it becomes a new data type in Apex. Classes contain variables, methods, properties, constructors, initializers, and inner classes. These components are summarized in the following list, and their usage is demonstrated in Listing 4.22:
Variables—Variables hold the state of an object instance or class. By default, variables declared inside a class are scoped to individual object instances and are called member variables. Every instance of an object gets its own member variables and can read and write their values independently without interfering with the values stored in other object instances. There are also class variables, also known as static variables. They are declared using the
static
keyword. Static variables are shared across all instances of the object.
Methods—Methods define the verbs in a class, the actions to be taken. By default, they operate within the context of individual object instances, able to access all visible member variables. Methods can also be static, operating on the class itself. Static methods have access to static variables but never member variables.
Properties—A property is a shortened form of a method that provides access to a static or instance variable. An even shorter form is called an automatic property. These are properties with no code body. When no code is present, the logic is implied. Getters return their value; setters set their value.
Constructors—A constructor is a special method executed when a class is instantiated. Constructors are declared much like methods, but share their name with the class name, and have no return type declaration.
Initializers—An initializer contains code that runs before any other code in the class.
Inner classes—An inner class is a class defined within another class.
class MyClass {
static Integer count; /* Class variable */
Integer cost; /* Member variable */
MyClass(String c) { /* Constructor */ }
void doSomething() { /* Method */ }
Integer unitCost { get { return cost; } set { this.cost = value; } }
Integer q { get; set; }
{ /* Initializer */ }
class MyInnerClass { /* Inner class */ }
}
Tip
Code listings containing static variables or inner class declarations cannot be tested in the Execute Anonymous View of the Force.com IDE. Create a stand-alone class and then invoke it from the Execute Anonymous view. To create a stand-alone class in the Force.com IDE, select your Force.com Project and then select New, Apex Class from the File menu.
Class definitions include notation to limit the visibility of their constituent parts to other code. This information-hiding notation protects a class from being used in unanticipated and invalid ways and simplifies maintenance by making dependencies explicit. In Apex, information hiding is accomplished with access modifiers. There are two places to use access modifiers: on classes, and on methods and variables:
Classes—An access modifier of
public
makes a class visible to the entire application namespace, but not outside it. A global
class is visible to Apex code running in every application namespace.
Methods and variables—If designated
private
, a method or variable is visible only within its defining class. This is the default behavior. An access modifier of protected
is visible to the defining class and subclasses, public
is visible to any Apex code in the same application namespace but not accessible to other namespaces, and global
can be used by any Apex code running anywhere in the organization, in any namespace.
Apex supports interfaces, which are skeletal class definitions containing a list of methods with no implementation. A class built from an interface is said to implement that interface, which requires that its method names and the data types of its argument lists be identical to those specified in the interface.
The proper use of interfaces can result in modular programs with clear logical boundaries between components, making them easier to understand and maintain.
Apex supports single inheritance. It allows a class to extend one other class and implement many interfaces. Interfaces can also extend one other interface. A class extending from another class is referred to as its subclass.
For a class to be extended, it must explicitly allow it by using the virtual
or abstract
keyword in its declaration. Without one of these keywords, a class is final and cannot be subclassed. This is not true of interfaces because they are implicitly virtual.
By default, a subclass inherits all the functionality of its parent class. All the methods defined in the parent class are also valid on the subclass without any additional code. This behavior can be selectively overridden if the parent class permits. Overriding a method is a two-step process:
1. The parent class must specify the virtual
or abstract
keywords on the methods to be overridden.
2. In the subclass, the override
keyword is used on the virtual or abstract methods to declare that it’s replacing the implementation of its parent.
After it’s overridden, a subclass can do more than replace the parent implementation. Using the super
keyword, the subclass can invoke a method in its parent class, incorporating its functionality and potentially contributing its own.
An object that inherits a class or implements an interface can always be referred to in Apex by its parent class or interface. References in variable, property, and method declarations treat the derived objects identically to objects they are derived from, even though they are different types.
This polymorphic characteristic of object types can help you write concise code. It works with the hierarchy of object types to enable broad, general statements of program behavior, behavior applying to many object types at once, while preserving the option to specify behavior per object type.
One example of using polymorphic behavior is method overloading, in which a single method name is declared with multiple argument lists. Consumers of the method simply invoke it by name, and Apex finds the correct implementation at runtime based on the object types.
52.15.161.188