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();
}
}