Key Differences Between Abstract Classes and Interfaces in Salesforce Apex

Key Differences Between Abstract Classes and Interfaces in Salesforce Apex
Chinmaya By Chinmaya
5 Min Read

Introduction

When working with abstraction in Salesforce Apex, you’ll often use abstract classes and interfaces. Both are used to define blueprints for classes, but they serve different purposes and have distinct characteristics. Understanding the differences between them is crucial for designing clean, maintainable, and scalable code.

Let’s break down the key differences between abstract classes and interfaces:

1. Definition and Purpose

Abstract Class:

    • An abstract class is a class that cannot be instantiated on its own. It serves as a base class for other classes.

    • It can contain both abstract methods (methods without implementation) and concrete methods (methods with implementation).

    • It is used to provide a common base implementation that can be shared across multiple subclasses.

Interface:

    • An interface is a contract that defines a set of methods that a class must implement.

    • It cannot contain any method implementations—only method signatures (i.e., the method name, return type, and parameters).

    • It is used to enforce a specific behavior across unrelated classes.

2. Method Implementation

Abstract Class:

  • Can have both abstract and concrete methods.

    • Abstract methods: Must be overridden by subclasses.

    • Concrete methods: Can be inherited and used directly by subclasses.

  • Example:

				
					public abstract class Animal {
    // Abstract method
    public abstract void makeSound();

    // Concrete method
    public void sleep() {
        System.debug('Sleeping...');
    }
}
				
			

Interface:

  • Can only have method signatures (no implementation).

  • All methods in an interface are implicitly abstract and public.

  • Example:

				
					public interface Drivable {
    void start();
    void stop();
}
				
			

3. Variables

Abstract Class:

    • Can have instance variablesstatic variables, and constants.

    • Variables can have any access modifier (publicprivateprotected, etc.).

    • Example:

				
					public abstract class Vehicle {
    protected String model;
    public static final String TYPE = 'Vehicle';
}
				
			

Interface:

    • Can only have constants (i.e., public static final variables).

    • Variables in an interface are implicitly publicstatic, and final.

    • Example:

				
					public interface Notification {
    String DEFAULT_MESSAGE = 'Hello!';
}
				
			

4. Inheritance

Abstract Class:

    • A class can extend only one abstract class (single inheritance).

    • Subclasses inherit both the abstract and concrete methods of the abstract class.

    • Example:

				
					public class Car extends Vehicle {
    public override void start() {
        System.debug('Car started.');
    }
}
				
			

Interface:

    • A class can implement multiple interfaces (multiple inheritance).

    • The class must provide implementations for all methods defined in the interface(s).

    • Example:

				
					public class SmartCar implements Drivable, Electric {
    public void start() {
        System.debug('Smart car started.');
    }
    public void stop() {
        System.debug('Smart car stopped.');
    }
    public void charge() {
        System.debug('Smart car charging.');
    }
}
				
			

5. When to Use

Abstract Class:

  • Use an abstract class when:

    • You want to provide a common base implementation for subclasses.

    • You need to share code or behavior among multiple related classes.

    • You want to define non-public members (e.g., protected or private methods/variables).

    • You need to include both abstract and concrete methods.

Interface:

  • Use an interface when:

    • You want to define a contract that multiple unrelated classes must follow.

    • You need to enforce specific behaviors across different classes.

    • You want to support multiple inheritance (since Apex doesn’t support multiple inheritance for classes).

    • You don’t need to provide any method implementations.

6. Example to Illustrate Differences

Abstract Class Example:

				
					public abstract class Shape {
    // Abstract method
    public abstract Double getArea();

    // Concrete method
    public void display() {
        System.debug('This is a shape.');
    }
}

public class Circle extends Shape {
    private Double radius;

    public Circle(Double radius) {
        this.radius = radius;
    }

    public override Double getArea() {
        return 3.14 * radius * radius;
    }
}
				
			

Interface Example:

				
					public interface Drawable {
    void draw();
}

public class Circle implements Drawable {
    public void draw() {
        System.debug('Drawing a circle.');
    }
}

public class Square implements Drawable {
    public void draw() {
        System.debug('Drawing a square.');
    }
}
				
			

Summary of Key Differences

FeatureAbstract ClassInterface
InstantiationCannot be instantiated directly.Cannot be instantiated directly.
Method ImplementationCan have both abstract and concrete methods.Can only have method signatures (no implementation).
VariablesCan have instance variables, static variables, and constants.Can only have constants (public static final).
InheritanceA class can extend only one abstract class.A class can implement multiple interfaces.
Use CaseUse when you need a common base implementation.Use when you need to enforce a contract across unrelated classes.

Conclusion

Both abstract classes and interfaces are powerful tools for achieving abstraction in Salesforce Apex. The choice between them depends on your specific use case:

    • Use abstract classes when you want to share code and provide a common base for related classes.

    • Use interfaces when you want to define a contract and enforce specific behaviors across unrelated classes.

By understanding these differences, you can make better design decisions and write cleaner, more maintainable code. Happy coding! 🚀

Share This Article
Follow:
Chinmaya is working as a Senior Consultant with a deep expertise in Salesforce. Holding multiple Salesforce certifications, he is dedicated to designing and implementing cutting-edge CRM solutions. As the creator of Writtee.com, Chinmaya shares his knowledge on educational and technological topics, helping others excel in Salesforce and related domains.
1 Comment