Frage

I have an interface (p) and an implementation (imp). If I do the following in the code, then the check works:

if (!(imp instanceof p)) {
   logger.error(imp.getClass().getName()+ 
    " doesn't satisfy the interface "
   +p.getClass().getName());
   }

I tried to make it into a callable method as follows:

private boolean checkInterfaceImplementation(Object implemen, Object inter){
    return (implemen instanceof inter);
}

which failed.

Then I found that inter needs to be a specific type and I cannot use a generic object. Then I found out about

"B.class.isAssignableFrom(A.getClass())"

Then I did:

System.out.println("B.class.isAssignableFrom(A
                    .getClass())");

The output was

true

I read up more from this question. My question is "Is this (the second implementation with ".isAssignableFrom") the preferred or standard way to implement said method? Is there any way that this present implementation can create problems?

War es hilfreich?

Lösung

It's hard to understand your question. I will edit when more details are received, if necessary, but as a general rule of thumb instanceof is an indicator of a lack of polymorphism and a design issue. Not always the case, but if you are a beginner I would try to use it as little as possible.

Instead, consider why that check is even there. If "imp" implements "p", then you are guaranteeing that any "imp" will have all the methods in "p". If it doesn't, you will receive a compiler error before you can even build. This is very abstract right now so I will do quick example.

public interface Runs {
     public void run();
}

public class Cat implements Runs {
     int numLegs;
     public Cat() {
          this.numLegs = 4;
     }
     public void run() {
          System.out.println("does whatever running cats do");
     }
}

public class Human implements Runs {
     int numLegs;
     public Human() {
          this.numLegs = 2;
     }
     public void run() {
          System.out.println("does whatever running humans do");
     }
}
 public class Main {
         public static void main(String[] args) {
              Cat cat = new Cat();
              Human human = new Human();

              ArrayList<Runs> listOfRunners = new ArrayList<Runs>();
              listOfRunners.add(cat);
              listOfRunners.add(human);
              Runs runner = listOfRunners.get(0);
              /* no compiler error because by implementing Runs we guarantee it has run() method */
              runner.run();
              runner = listOfRunners.get(1);
              /* It doesn't matter what the object is. We don't care if it is cat or human */
              runner.run();
         }
    }

Andere Tipps

Not sure exactly what you are trying to do, but something like this should work:

inter.getClass().isInstance(implemen)

Most likely what you are trying to do can be done in a much better way than resorting to this, though.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top