There is a method to which an object of any class can be passed as an argument, this method should check by means of instanceof to which class the object to be transferred belongs:

public class Alligator implements Predator {} public class Panda implements Herbivore {} public class Check{ Object naturalEnemy = new Alligator();// Проверяемый по умолчанию класс, !!!!может изменятся!!!!!! public static void main(String[] args){ System.out.println(""+checkAnimal(new Alligator()));// System.out.println(""+checkAnimal(new Panda())); } public boolean checkAnimal(Object animal){ if(naturalEnemy instanceof animal.getClass()) return true;// Не работает :( //if(naturalEnemy instanceof Predator) return true;// Работает, но подобный подход не приемлим. } } 

The catch is that none of the objects being compared is known, and it is also not acceptable to output the class in the text line. Help who than can. Thank you in advance.

  • one
    Where does the restriction come from? What is the real problem you are solving? - VladD
  • Let me explain in more detail: in order to make it easy to check, I created many interfaces: predator / herbivore (or both), flying / amphibian / ground. An animal’s natural enemy is transmitted to the animal by scanning the space, so the best way to check it would be an “instanceof” operator, it checks the class itself and all interfaces implemented by it. Of course, you can convert classes to a text string and compare, but the amount of code will increase significantly. - arachnoden
  • one
    Why are you handing a copy of the natural enemy? You must pass the class itself, as far as I understand, the instance of this should work quite correctly for itself. - etki
  • @Etki instanceof is not intended for this. Here the class to check is dynamic . But it would be more correct to transfer the class, and not the class object - cache
  • one
    @Etki; We can say obj instanceof java.lang.String , but we can't Class<?> myclass = String.class; obj instamceof myclass; Class<?> myclass = String.class; obj instamceof myclass; (the code simply won't compile). But we can check myclass.isInstance(obj) ; so, for this check we don’t need a class instance, the class itself is enough, as you said - cache

2 answers 2

UPD 3: Instead of instanceof use Class.isInstance . instanceof not applicable here

That is your check

 if(naturalEnemy instanceof animal.getClass()) return true; 

need to be replaced by

 return naturalEnemy.getCLass().isInstance(animal); 

or at

 animal.getCLass().isInstance(naturalEnemy); 

depending on which animal you are testing

  • It helped, thanks. - arachnoden

make the abstract class abstract class Animal and all animals inherit from it.

 public class Predator extends Animal 

and then check

 if(naturalEnemy instanceof Animal) 
  • In the original program it is, all classes of animals are inherited from one, but then as they decrease they become clouded with predator and herbivore interfaces, and each has its own specific natural enemy, therefore (for example) in the Panda class the link to the object into which the predator is transmitted, then when reviewing the terrain the panda must compare the animal found with its natural enemy - arachnoden
  • but since the object "natural enemy" is defined in the parent class "Animal", the natural enemy for each animal is determined through the constructor, therefore neither the natural enemy class nor the class of the detected animal can be determined in advance. - arachnoden