Retrieve all the implementations of an interface in Java

0 votes

Can someone explain how to retrieve the entire list of the implementations of an interface in Java?

Jul 23, 2018 in Java by 93.lynn
• 1,550 points

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

Hi...I think you can achieve this by using the following:

  1. Reflections: As Reflections help you to perform the following actions:
    • get all subtypes of some type
    • get all types annotated with some annotation 
    • get all types annotated with some annotation, including annotation parameters matching
    • get all methods annotated with some
      Reflections reflections = new Reflections("firstdeveloper.examples.reflections");
      Set<Class<? extends Fruit>> classes = reflections.getSubTypesOf(Fruit.class);
  2. ServiceLoader 
    ServiceLoader<Fruit> loader = ServiceLoader.load(Fruit.class);
    for (Fruit implClass : loader) {
        System.out.println(implClass.getClass().getSimpleName());// prints Apple, Mango
    Do remember, you need to define Fruit as a ServiceProviderInterface (SPI) and declare its implementations. you do that by creating a file in resources/META-INF/services with the name examples.reflections.Fruit and declare all implementations of Fruit in it.
  3. package-level annotation

    Package[] packages = Package.getPackages();
    for (Package p : packages) {
        MyPackageAnnotation annotation = p.getAnnotation(MyPackageAnnotation.class);
        if (annotation != null) {
            Class<?>[]  implementations = annotation.implementationsOfPet();
            for (Class<?> impl : implementations) {

    and the annotation definition:

    public @interface MyPackageAnnotation {
        Class<?>[] implementationsOfPet() default {};

    and you need to declare the package-level annotation in a file named inside that package. here are sample contents:

    @MyPackageAnnotation(implementationsOfPet = {Apple.class, Mango.class})
    package examples.reflections;

    Note that only packages that are known to the ClassLoader at that time will be loaded by a call to Package.getPackages().

answered Jul 23, 2018 by anto.trigg4
• 3,420 points

Related Questions In Java

0 votes
2 answers

How can I separate the digits of an int number in Java?

You can convert a number into String ...READ MORE

answered Aug 10, 2018 in Java by Sushmita
• 6,840 points
0 votes
1 answer

Get all the permutations of a string in Java

This is the recursive way of finding a ...READ MORE

answered May 10, 2018 in Java by sharth
• 3,310 points
0 votes
1 answer

Listing all the subclasses of a specific class in Java

There is a simple library available for ...READ MORE

answered Jun 7, 2018 in Java by geek.erkami
• 2,320 points
0 votes
1 answer

What are all the different ways to create an object in Java?

There are four different ways to create ...READ MORE

answered Oct 16, 2018 in Java by Daisy
• 8,040 points
0 votes
2 answers

What is the difference between implements and extends?

Extends : This is used to get attributes ...READ MORE

answered Aug 3, 2018 in Java by samarth295
• 2,190 points
0 votes
1 answer

using comparator for sorting in java

You've got two problems: 1) You're using Collections.sort (which takes ...READ MORE

answered Jul 6, 2018 in Java by samarth295
• 2,190 points
0 votes
1 answer

Usage of abstract class and interface

Well let me draw a clear line ...READ MORE

answered Oct 23, 2018 in Java by geek.erkami
• 2,320 points
0 votes
1 answer

Number of Characters in Java String

I think you can make use of ...READ MORE

answered Oct 22, 2018 in Java by anto.trigg4
• 3,420 points
0 votes
1 answer

Setting time zone of a java.util.Date

You can make use of the following ...READ MORE

answered Aug 14, 2018 in Java by anto.trigg4
• 3,420 points

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.