How to know which object is the one on the list [closed]

0

Friends have an application that contains a class Animals from which the mammal class inherits with its attributes and from this inherit two carnivorous and herbivorous classes. Now in a zoo class I have a list of animals which will store two types of objects only (carnivores or herbivores). Now in the same zoological class I want in a method to accede if Carnivore is the object to a certain attribute and if it is Herbivore to an attribute of herbivore. How to make the program identify what object it is and give me its particular attribute. Thanks ...

    
asked by ErnestoPerez 03.05.2017 в 17:43
source

1 answer

1
if(object instanceof Herviboro){
  object.metodoHerviboro();
}else if(object instanceof Carnivoro){
  object.metodoCarnivoro();
}

In this way you check if an object is an instance of a certain class.

    
answered by 03.05.2017 в 17:50