Check that the instance is of the expected class before casting