Reputation:
how do one narrow down instanceof
to a specific child without ending up with the parent class .
for example if i should have a parent class StackoverflowUsers
then i have direct subclass of TenKUsers
,HundredKusers
etc etc.. Now my question is how do i use instanceof
to get if an Object
is an instance of one of the direct subclasses of StackoverflowUsers
and not end up with the option of that my object
is an instance of StackoverflowUsers
.. is that possible?
Upvotes: 2
Views: 440
Reputation: 21576
You can exclude the superclass itself with checking .getClass()
, but "grandchildren" will also be matched...
You could use reflection, but in general reflection should be avoided (is kind of "cheating").
public class T {
static class StackoverflowUsers {}
static class TenKUsers extends StackoverflowUsers {}
static class HundredKusers extends StackoverflowUsers {}
static class EtcEtc extends TenKUsers {}
public static void main(String[] args) {
System.out.println("Instance of String: "+checkInstance(new String()));
System.out.println("Instance of StackoverflowUsers (superclass): "+checkInstance(new StackoverflowUsers()));
System.out.println("Instance of TenKUsers (direct subclass): "+checkInstance(new TenKUsers()));
System.out.println("Instance of HundredKusers (direct subclass): "+checkInstance(new HundredKusers()));
System.out.println("Instance of EtcEtc (indirect subclass): "+checkInstance(new EtcEtc()));
System.out.println();
System.out.println("using reflection:");
System.out.println("Instance of String: "+checkInstanceUsingReflection(new String()));
System.out.println("Instance of StackoverflowUsers (superclass): "+checkInstanceUsingReflection(new StackoverflowUsers()));
System.out.println("Instance of TenKUsers (direct subclass): "+checkInstanceUsingReflection(new TenKUsers()));
System.out.println("Instance of HundredKusers (direct subclass): "+checkInstanceUsingReflection(new HundredKusers()));
System.out.println("Instance of EtcEtc (indirect subclass): "+checkInstanceUsingReflection(new EtcEtc()));
}
private static boolean checkInstance(Object o) {
return (o instanceof StackoverflowUsers) && (o.getClass() != StackoverflowUsers.class);
}
private static boolean checkInstanceUsingReflection(Object o) {
Class<?> superclass = o.getClass().getSuperclass();
return superclass == StackoverflowUsers.class;
}
}
which returns:
Instance of String: false
Instance of StackoverflowUsers (superclass): false
Instance of TenKUsers (direct subclass): true
Instance of HundredKusers (direct subclass): true
Instance of EtcEtc (indirect subclass): true
using reflection:
Instance of String: false
Instance of StackoverflowUsers (superclass): false
Instance of TenKUsers (direct subclass): true
Instance of HundredKusers (direct subclass): true
Instance of EtcEtc (indirect subclass): false
Maybe you should rethink your classes. It seems like TenKUsers
could also be a boolean flag stored inside of StackoverflowUsers. Or even better: give your StackoverflowUsers an EnumSet<Archievments>
and make Archievments contain "TenKUser" and "HundredKusers".
Upvotes: 2
Reputation: 3992
In addition to the answer of Mena, if you want to use instanceof
to check, you can make your superclass Stackoverflowusers
abstract to ensure no instances can exist and then check with:
object instanceof Stackoverflowusers
Upvotes: 0
Reputation: 48404
Simply use the instanceof TenKUsers
(etc.) idiom.
It will tell you if the object you are looking at is the child of that base class.
Since you imply TenKUsers
is a child of StackoverflowUsers
, instanceof StackoverflowUsers
will also be true
for that object, of course.
Upvotes: 4