Virtual Methods
In Apex, a virtual method is a method declared in a base class that can be overridden in its subclasses. This allows you to provide different implementations of the method in different subclasses. This concept is useful when you want to define a common behavior in a base class, but allow its subclasses to customize or extend that behavior.
In the given example, we start with a base class named Item, which defines a virtual method called calculateProperty(). The intention is to create specialized subclasses, such as Car and Bicycle, each offering its own unique implementation of the calculateProperty() method.
public class Item {
public virtual void calculateProperty() { System.debug(‘Calculating some property of the item’);
}
}
public class Car extends Item {
private Decimal fuelEfficiency;
public Car(Decimal fuelEfficiency) { this.fuelEfficiency = fuelEfficiency;
}
public override void calculateProperty() { Decimal distance = 100;
// Assume a constant distance for simplicity
Decimal fuelConsumed = distance / fuelEfficiency;
System.debug(‘Calculating fuel consumption of the car: ‘ + fuelConsumed);
}
}
public class Bicycle extends Item {
private Decimal distanceTraveled;
public Bicycle(Decimal distanceTraveled) { this.distanceTraveled = distanceTraveled;
}
public override void calculateProperty() {
System.debug(‘Calculating distance traveled by the bicycle: ‘ +
distanceTraveled);
}
}
// Usage
Item genericItem = new Item();
genericItem.calculateProperty();
// Output: Calculating some property of the item
Item car = new Car(10); // Car with fuel efficiency 10 car.calculateProperty();
// Output: Calculating fuel consumption of the car: 10.0
Item bicycle = new Bicycle(50); // Bicycle with 50 km distance traveled bicycle.calculateProperty();
// Output: Calculating distance traveled by the bicycle: 50.0
When we instantiate an object from the Item class and invoke the calculateProperty() method, the output is “Calculating some property of the item,” reflecting the generic nature of the base class.
On the other hand, creating instances of the Car and Bicycle classes and calling their respective calculateProperty() methods yields distinctive outputs. For a Car instance with a fuel efficiency of 10, invoking calculateProperty() results in “Calculating fuel consumption of the car: 10.0.” Similarly, for a Bicycle instance that has traveled 50 km, invoking calculateProperty() produces “Calculating distance traveled by the bicycle: 50.0.”
This example illustrates how the use of a virtual method allows for the encapsulation of diverse calculations, showcasing the flexibility of the object-oriented paradigm in tailoring behavior based on specific subclasses.
Global Methods
A global method has the highest level of visibility and can be accessed from other classes, triggers, and even external systems, like managed packages or web services. They are often used to define entry points for integration with external systems.
Step 1: Create the class and save
global class MyGlobalClass {
global Integer myGlobalMethod() {
// Your code here
return 5; // Example return statement
}
}
In this example, the MyGlobalClass has a global method myGlobalMethod that returns an integer value.
Step 2: Create another class and save
public class MyCallingClass {
public static void callGlobalMethod() {
MyGlobalClass myGlobalInstance = new MyGlobalClass(); Integer result = myGlobalInstance.myGlobalMethod(); System.debug(‘Result: ‘ + result); // Example debug statement
}
}
In this example, the MyCallingClass has a public static method callGlobalMethod.
This method creates an instance of MyGlobalClass and calls the myGlobalMethod on it.
The result of the myGlobalMethod is stored in the result variable and debugged.
Step 3: Open the anonymous window
Step 4: Test the global class
MyCallingClass.callGlobalMethod();
This will execute the myGlobalMethod from the MyGlobalClass and print the result in the debug log.
Remember that global methods have some limitations and considerations. They can only be used in certain contexts, such as in managed packages or with certain APIs. Additionally, global methods can expose sensitive data if not properly secured, so make sure to implement any necessary access controls and validations within the method itself.