isAssignableFrom() vs. instanceof

In Java, you can check the class of an object using the instanceof operator:

microwave instanceof Oven

When you have a Class object, it would make sense to write:

microwave.getClass().isAssignableTo(Oven.class)

Unfortunately, there is no isAssignableTo. It has to be written the other way round:

Oven.class.isAssignableFrom(microwave.getClass())
Ralf Ebert

Ralf Ebert is an independent software developer and trainer. He makes apps for Mac OS X and iOS and offers iOS and Git training courses for software developers.