I have a managed package 'A' which has a global interface with a method

global interface Ivehichle {

string getEngineName() ;


In the managed package B, i am implementing this interface Ivehicle in a public class

public with sharing class car implements PackageANameSpace.IVehicle {
public String getEngineName() {
   return '';


Now in my managed package A when i try to retrieve the type and instance of the managed package B class

Type t = Type.forName('NameSpaceofPackageB','car');
IVehicle vehicle = (Ivehicle)t.newinstance();

the type.forname throws a null pointer exeption

2 Answers 2


That's expected, as you can't directly access public classes in that manner. You'll want a global class you can call. To avoid exposing implementation details, the method itself can be public.

You probably want to just create a Factory instead. At a very basic level...

global class Factory implements Callable {
  public Object call(String action, Map<String, Object> args) {
    switch on action {
      when 'getCar' { return new Car(); }
      when else { return null; }

You can leave car as public with this implementation:

// From package A
Callable factory = (Callable)Type.forName('PackageBNamespace','Factory').newInstance();
IVehicle car = factory.call('getCar',null);

The class car is declared public. As such, it is not exposed in the context of managed package A, regardless of the fact that it implements an interface defined in that managed package.

If you want car to be accessible in package A, declare it as global.

Otherwise, you would need to build these packages as second-generation managed packages and make use of the @namespaceAccessible annotation. That feature does not apply to first-generation managed packages.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .