How can we find the no of classes implementing the particular interface

Posted on 2004-11-09
Last Modified: 2010-03-31
lets say there is an interface that has a method but the implementation is done in many classes. how can we know the implementation classes
Question by:chaitu chaitu
    LVL 86

    Expert Comment

    You'd have to iterate them all, load the classes and try casting them to that interface
    LVL 35

    Expert Comment

    Do you mean to find out what class implements the interface? You can use the instanceof operator.
    LVL 37

    Accepted Solution

    If you have all possible classes available:

    List allPossibleClasses = ...;
    Iterator it = allPossibleClasses.iterator();
    while (it.hasNext()) {
       Class c = (Class);
       if (c instanceof BlahblanInterface)
          System.out.println( c.getName() );

    But, I guess, getting all possible classes is the problem, isn't it?
    LVL 24

    Expert Comment

    This question is not clear:
    1) which of a set of classes implements the interface? > look at the JAVADOC documentation of the set,
    2) which of a set .java files does it? > use search with context "implements Interface"
    (not safe when implementing multiple intercases.
       You could use grep with "class.*implements.*Interface" on LINUX to overcome this)
    If you want to detect this runtime the former answers could be used, but I think that's
    a weird time to do it.
    I assume you want to know which classes are usable, and that's even before compile time.
    LVL 86

    Expert Comment

    >>I guess, getting all possible classes is the problem, isn't it?

    LOL - obviously not
    LVL 37

    Expert Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Live - One-on-One Java Help from Top Experts

    Solve your toughest problems, fast.
    Java experts are online now and ready to help you.

    Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
    Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now