function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Nick VerschuerenNick Verschueren 

Updating interface class in managed package

We have a managed package that contains a few global interface classes. These classes are extended within the package but can also be extended after installation in the target org.

These interfaces are chained.

Interface D extends interface C extends interface B extends interface A, which is the base Interface. 

And Class D implements interface D
Class C implements interface C
And so on, you get the picture...

All interface classes are used as implementations.

But now we want to add methods to one of the interfaces, let's say B in our example.

This is not possible since we can't add new methods to an interface class. But we need ot inject some methods here for new funcionality.

What would be the best way to go about this? We can't just add them to Class B because we cast the class instances as an instance of the interfaces and then we get an error that object of type interface B doesn't have the method we just added, which makes sense since it's not added to the interface class itself...

Best Answer chosen by Nick Verschueren
Prateek Prasoon 25Prateek Prasoon 25
Adding methods to an existing interface is not possible in Salesforce, as interfaces are fixed contracts that cannot be modified after they have been defined.
However, one way to work around this limitation is to define a new interface that extends the existing interface, and add the new methods to the new interface.
For example, in your case, you could define a new interface B2 that extends B, and add the new methods to B2. Then, any classes that need to use the new methods would implement both B and B2.
Here's an example of how you could define the new interface and update your classes:
Here's an example of how you could define the new interface and update your classes:
// Define the new interface B2
global interface B2 extends B {
    void newMethod();
}

// Update the classes to implement both B and B2
global class D implements D, B2 {
    // Implement the methods from D and B2
}

global class C implements C, B2 {
    // Implement the methods from C and B2
}

global class B implements B {
    // Implement the methods from B
}

global class A implements A {
    // Implement the methods from A
}

In this example, B2 extends B and adds a new method newMethod(). The D and C classes are updated to implement both B and B2, while the B and A classes remain unchanged.
This approach allows you to add new functionality without modifying the existing interfaces or classes, and without breaking any existing code that relies on those interfaces.

If you find this answer helpful,Please mark it as the best answer