Java object sorting

Here is my dilemma
I want to create an even system for my game engine
The only way I can imagine doing it is like this

Where Event and Event Listener are objects that superclass things like "KeyDownEvent" allowing for information to be parsed with the object with ease.

The only issue I have is that how am I meant to sort the different event objects programatically. Like tell the difference between a KeyDownEvent and KeyUpEvent?

I can't figure it out. If somone has a better idea please notify me.

I figured it out. I was assuming comparing object1.getClass() and object2.getClass() would serve true if they both shared the same super but I was wrong.

Hopefully someone finds this ramble useful


You should use 'instanceof'. For example:

If (subclassOne instanceof SuperClass && subclassTwo instanceof SuperClass) 
    // Do something cool.

Continuing the discussion from Java object sorting:

Two things I don't think you understood my original predicament (I find it very hard to explain myself) I wanted to sort the two different classes apart, even though they had the same superclass.

Instanceof is too hardcoded you can't dynamically change what's being compared. The code I found can

Yeah I was specifically replying to:

Instanceof will solve that problem, I know you've already solved you're problem, but it's for someone else who might read this looking for a solution.

The dynamic equivalent is Class.isInstance()