Java tutorial
//package com.java2s; public class Main { @SuppressWarnings("unchecked") public static <T> T objectCast(Object obj, Class<T> classToCast) { if (obj == null) return null; Class<? extends Object> curClass = obj.getClass(); if (classToCast.isAssignableFrom(curClass)) return (T) obj; return null; } }