Sairam Mohan
Sairam Mohan

Reputation: 37

Check which class

I have an object of a child class and an array of the parent class that stores objects of the child classes in it.

mafia[] obj = new mafia[no_mafia];
detective[] obj1 = new detective[no_detectives];
healer[] obj2 = new healer[no_healers];
commoner[] obj3 = new commoner[no_commoners];

the parent class is called player and basically stores each individual objects of each of its child class in it

alive_players[0] = obj[0];//just an example

Now I want to check if the classes of each of their objects are the same for eg:

obj[0].equals(alive_players[rand_var]);

But this doesn't seem to work as they're all subclasses of the class player(which is the parent class). How exactly will I be able to differentiate between their classes? And is there a better way to store all the objects of the subclasses in a common place?

Upvotes: 1

Views: 49

Answers (1)

Tahero
Tahero

Reputation: 328

You need to use Java instanceof: The java instanceof operator is used to test whether the object is an instance of the specified type

Upvotes: 1

Related Questions