I’m working on a java project and I have two class (for this topic, not in the project) : the class “Objet” and the class “Cle extends Objet” with the method “getNumero()” only in Cle. In my main, I wrote this code :
Objet[] objets = Scenario.objets;
for (int i=0;i<objets.length;i++)
{
if(objets[i].getClass() == modele.Cle.class
&& objets[i].getNumero() == salle.getPorte(direction).getNumero()){}
}
The problem is in the second part of the “if”, the class Objet hasn’t a method “getNumero()”, so I check if the Objet is a Cle, but java doesn’t understand that if my first part of the “if” is true, the second part is possible. Can someone say if it’s possible to extract a Cle from objets, and how ?
Thank you.
Use an explicit cast, the common idiom is:
Looking further and depending on the concrete situation you could add
getNumero()as an abstract method toObjet, instead of casting. If you cannot do that, maybe you could look into Generics to enable your supplier ofObjetinstance to provideCleinstances. That again depends on the concrete scenario.