29

Im overriding an equals() method and I need to know if the object is an instance of a Event's subclass (Event is the superclass). I want something like "obj subclassof Event". How can this be made?

Thanks in advance!

rasgo
  • 1,381
  • 4
  • 15
  • 28
  • 10
    What's wrong with `instanceof` which caused you to ask this question? Please post some code with your attempts, expectations and unexpectations. – BalusC Apr 23 '10 at 15:29

6 Answers6

53

instanceof can handle that just fine.

Ken Bloom
  • 57,498
  • 14
  • 111
  • 168
38

With the following code you can check if an object is a class that extends Event but isn't an Event class instance itself.

if(myObject instanceof Event && myObject.getClass() != Event.class) {
    // then I'm an instance of a subclass of Event, but not Event itself
}

By default instanceof checks if an object is of the class specified or a subclass (extends or implements) at any level of Event.

Draken
  • 3,134
  • 13
  • 34
  • 54
Adrian
  • 2,244
  • 18
  • 20
  • 2
    Why thank you sir :-) The other way to do it is to make Event abstract, or an interface, and not bother checking that your object is specifically an instance of it, because it can't be. – Adrian Apr 23 '10 at 15:43
10

Really instanceof ought to be good enough but if you want to be sure the class is really a sub-class then you could provide the check this way:

if (object instanceof Event && object.getClass() != Event.class) {
    // is a sub-class only
}

Since Adrian was a little ahead of me, I will also add a way you could do this with a general-purpose method.

public static boolean isSubClassOnly(Class clazz, Object o) {
    return o != null && clazz.isAssignableFrom(o) && o.getClass() != clazz;
}

Use this by:

if (isSubClassOnly(Event.class, object)) {
    // Sub-class only
}
Kevin Brock
  • 8,874
  • 1
  • 33
  • 37
  • Maybe also have ... && clazz != null && ... just for completeness? :D – Paggas Apr 25 '10 at 22:06
  • 1
    @Paggas: I would consider that a programming error and thus want to have the NullPointerException. Basically it's not possible to have `object instanceof null` so why allow `isSubClassOnly(null, object)`. – Kevin Brock Apr 26 '10 at 15:36
3

You might want to look at someObject.getClass().isAssignableFrom(otherObject.getClass());

Mark Bolusmjak
  • 23,606
  • 10
  • 74
  • 129
  • That's only useful if you have only class information. But in the `equals()` you have the both **instances** to your availability. – BalusC Apr 23 '10 at 15:30
  • Yes, you're right. I used the word 'might' for a reason. e.g. Writing a reusable "equalsHelper" method that does some preliminary checks for you. The correct information had already been posted so I just wanted to add some other consideration. – Mark Bolusmjak Apr 23 '10 at 15:35
1

There is no direct method in Java to check subclass. instanceof Event would return back true for any sub class objects

The you could do getClass() on the object and then use getSuperclass() method on Class object to check if superclass is Event.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
Fazal
  • 2,991
  • 7
  • 29
  • 37
0

If obj is a subclass of Event then it is an instanceof. obj is an instanceof every class/interface that it derives from. So at the very least all objects are instances of Object.

DaveJohnston
  • 10,031
  • 10
  • 54
  • 83