I have the following scenario:
public class A { } public class B extends A { } public class C extends B { public void Foo(); } I have a method that can return class A, B or C and I want to cast safely to C but only if the class type is C. This is because I need to call Foo() but I don't want the ClassCastException.
instanceof, have a look at posts like this, as usinginstanceofis often a sign of a design flaw: stackoverflow.com/questions/2750714/… and stackoverflow.com/questions/2790144/avoiding-instanceof-in-java