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
Nicolas VuillamyNicolas Vuillamy 

How to list inheriting classes from an abstract class

Hi ,

I'm struggling with SF docs / forums / google and I don't succeed to find a response to a simple question:

How to list classes inheriting from a super class ?

Example :
 
public abstract MyRootAbstractClass
{
      public virtual String getLabel() {return null ;}
}

public class MyFirstChildClass extends MyRootAbstractClass
{
      public override String getLabel() {return 'MyFirstChildClassLabel' ;}
}

public class MySecondChildClass extends MyRootAbstractClass
{
      public override String getLabel() {return 'MySecondChildClassLabel' ;}
}

I need to be able to create a method who can return 'MyFirstChildClassLabel','MySecondChildClassLabel'

I anyone is able to fill the blank part in my code, I would be enternally thankful :)
 
        Type RootClassType = Type.forName('MyRootAbstractClass');
        /////
        // WHAT TO PUT HERE ??? :'( 
        /////
        List<String> AvailableChildClassList = new List<String>();
        for (String ChildClass : ChildClassList)
        {
            ClassType = Type.forName(ChildClass);
            SomeInterface ClassInstance = (SomeInterface)ClassType.newInstance();
            String ChildClassLabel = ClassInstance.getLabel();
            AvailableChildClassList.add(ChildClassLabel);
        }
        return AvailableChildClassList;

Note: some code parts are missing about interface management but it's ok i know how to manage it :)

Many thanks , best regards & have a nice day :)
Amit GhadageAmit Ghadage
Hi Nicolas Vuillamy,
This may be late reply..

 
List<String> getInheritedClassNames(String abstractClassName){

List<String> inheritedClassesList = new List<String>();
List<List<SObject>> searchList = [FIND :abstractClassName IN ALL FIELDS 
                                      RETURNING ApexClass(Name)];
List<ApexClass> apexClassesList = searchList[0];
for(ApexClass ac : apexClassesList )
{
inheritedClassesList .add(ac.Name)
}
return inheritedClassesList ;
}

Best Regards.
Amit Ghadage