Util method to cast object to another class