Steve Chambers Steve Chambers - 11 months ago 43
Java Question

How to find all (child) sub-interfaces of a particular interface in Java?

Given an interface:

public interface A {};

with inheriting interfaces:

public interface B extends A {}

public interface C extends A {}

How can I programmatically scan to find
? I.e. how to do this:

Type[] types = findAllSubInterfacesOfA(); // Returns [B.class, C.class]

Note: Am trying to find interfaces here, not classes or instances.

Answer Source

Following snippet should do it.

public class FindSubinterfaces {

    public static void main(String[] args) {
        Class clazz = A.class;

        Reflections reflections = new Reflections(new ConfigurationBuilder()

        Set<Class<? extends List>> subTypes = reflections.getSubTypesOf(clazz);
        for (Class c : subTypes) {
            if (c.isInterface()) {
                System.out.println("subType: " + c.getCanonicalName());

interface A {

interface B extends A {

interface C extends A {

class CA implements A {

abstract class DC implements C {


subInterface: sub.optimal.reflections.B
subInterface: sub.optimal.reflections.C