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
shrutiiiiishrutiiiii 

Pass class instance name by String

I'm working on creating a managed package. In my managed package, there is a global virtual class and a method defined. I installed the beta version of package to a test organization and created an another class which is inherited from the package class. In this class I overriden the method.
e.g. Package class:
global class ManagedPackageInheritance { 
    global virtual class VirtualClass {
        global virtual void foo () {
            System.debug('#Foo from Managed Package !');
        }
    }
}

Inherited apex class in test org:
global class PlayVirtualInheritance extends namespace.ManagedPackageInheritance.VirtualClass {
   // override the virtual method
   global override void foo() {
        super.foo();
        System.debug('#Foo from Target Org !');
    }
}

So my question is, while calling the method 'foo', I want to pass the class instance name as a parameter. Something like this:

global class clsInvoke{
        public void invokeFoo(String className)       ///namespace.ManagedPackageInheritance.VirtualClass OR PlayVirtualInheritance
        {
                  //here call method foo() from the either class depending on the class name passed
        }
}

I think I need to use 'Type' class methods. But not sure how? I want to do this, because suppose the client/customer who has installed our package does not want the functionality provided by us, and they want to implement their own functionality. So the client will inherit our class and override the method. And then there is no need to modify the method invokation part.

Can anyone help me in this?
ShashForceShashForce
Please see if this helps: http://www.tgerm.com/2011/09/extend-managed-package-virtual-class.html
shrutiiiiishrutiiiii
I'm already referring this link. But I am not getting how to invoke the method in such a way that there is no need to change method invokation for different classes. What if my package code is invoking ManagedPackageInheritance foo() and the client wants to invoke PlayVirtualInheritance foo()? - without changing the method invokation/method call for different classe instances?

What do I do to invoke:
PlayVirtualInheritance obj = new PlayVirtualInheritance();
obj.foo();

Instead of this:
namespace.ManagedPackageInheritance.VirtualClass obj = new namespace.ManagedPackageInheritance.VirtualClass();
obj.foo();

Without changing the method calls? How do I pass obj instance name?
Jon SnowJon Snow
You can use Type class to instantiate your Apex class like the following source: 
Type t = Type.forName('ChildClassName');
ParentClass cls = (ParentClass)t.newInstance();
cls.callMethod();

FYI : https://www.xgeek.net/salesforce/instantiating-a-apex-class-based-on-dynamic-string-name-with-type-class/