+ Start a Discussion
philbophilbo 

Can you extend a managed package's virtual Apex class?

Hey,

I will be finding this out for myself in the next little while, but meanwhile:  Does anybody know whether the following is possible?

Say we have a managed package, with NS prefix 'XXX' and the following class:

global virtual class baseClass {

    WebService String baseString;

    public baseClass () {
        baseString = 'bsaeClass instantiated';
    }
}

 We have this package deployed on an org, and now we want to do the following:

global class myClass extends XXX.baseClass {

    WebService String myString;

    public myClass () {
        super ();  // --- not sure if this is necessary ---
        myString = 'myClass instantiated';
    }
}

Is this legal?  Can you extend a virtual base class, if that class is sitting inside a managed package?  Can you even include virtual classes within a managed package, for that matter?
TLFTLF

philbo,

 

I'm curious, how did you create your package containing global virtual classes? I cannot create global virtual classes because I'm working in a Developer Edition org. This is an annoying limitation, since I do my development and packaging in a DE org. Did you create your global virtual classes within a Enterprise Edition sandbox and then do you packaging from the sandbox?

sparkysparky

I would like to know the answer to this as well.  philbo, did you ever figure this out? 

 

Or could someone at SF set us straight?

 

BTW, for me the base class could be public, doesn't need to be global for my purposes.

 

Thanks much!

 

 

d3developerd3developer

Trying to do this now and it does not appear to be working

Abhinav GuptaAbhinav Gupta

This worked for me, this post explains all in detail : http://www.tgerm.com/2011/09/extend-managed-package-virtual-class.html

James S. CloudAnswers.comJames S. CloudAnswers.com
If you want to package a class and have methods be accessible from client code (not part of your managed package) then you need to make them globlal.  If you update your managed package to include new global methods, the client code will need to update the api versions used (in the -meta.xml file) to access those new methods/constructors.
Daniel Morton 15Daniel Morton 15
Thanks for your post Abhinav, even through its from 2011 it still helpful today!  There is not much information on abstract classes in managed packages.  

One issue that i faced that you noted in your blog was that "You cannot add a method to an abstract or virtual class after the class has been uploaded in a Managed - Released package version." 

This was the very first problem that we faced, as of course,  the first release missed things and we wanted to change the interface.  We were going to move away from a global abstract class, but found that you could add to them provided the added methods where also virtual. 

ie the following can be added:-
 
global virtual String getStatusMessage() {
      return getMessage();
    }
whereas a new method that isn't virtual can't be added and you get an error 
 
global  String getStatusMessage2() {
        return getMessage();
    }

-->  Cannot add new final methods to virtual or abstract classes


From - https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_manpkgs_package_versions.htm
 
Helman ReyesHelman Reyes
May be this information helps some one today (and as complement of @Daniel Morton 15  comment):

"You cannot override a public or protected virtual method of a global class of an installed managed package"

so for the question, if you want to override a method from a manage package, the method must have the "global" access modifier. It is not enough being only virtual neither public.