Skip to content
StackDecode
IntermediateLesson 21 of 37

Apex classes and object-oriented code

Classes, interfaces, inheritance and sharing.

apex
public with sharing class PricingService {
    private static final Decimal TAX = 0.2;

    public Decimal withTax(Decimal net) {
        return net * (1 + TAX);
    }
}

public interface Notifier { void send(String msg); }
public virtual class BaseNotifier implements Notifier {
    public virtual void send(String msg) { System.debug(msg); }
}
  • Access modifiers: private, protected, public, global.
  • with sharing makes the class respect the running user's record access (use it by default); without sharing ignores sharing; inherited sharing follows the caller.
  • Use custom exceptions (public class MyException extends Exception {}), Custom Metadata Types and Custom Settings for configuration.