instanceof
i understand the very basic gist of 'instanceof'. however, there are details i don't understand. to dive straight to my situation, consider the following code:
so, it appears to my relatively novice eyes that instanceof can test an instance of an object for ANY subtype, right?
i kinda don't get it, and i want to. does 'e' still have all of the "ArrayOutOfBoundException-specific" fields and methods, or does it lose them because it was "caught" as an "Exception"? i know AOOBE doesn't have much in the way of separate fields and methods from a normal Exception, but other classes may -- perhaps this code better illustrates:
would that work?
try {
asYouMight();
} catch (Exception e) {
if (e instanceof ArrayOutOfBoundsException) {
doSomethingVeryDifferent();
} else {
throw new fit(e);
}
}
so, it appears to my relatively novice eyes that instanceof can test an instance of an object for ANY subtype, right?
i kinda don't get it, and i want to. does 'e' still have all of the "ArrayOutOfBoundException-specific" fields and methods, or does it lose them because it was "caught" as an "Exception"? i know AOOBE doesn't have much in the way of separate fields and methods from a normal Exception, but other classes may -- perhaps this code better illustrates:
public void aMethod( SuperClass a ) {
if (a instanceof SuperClass) {
a.methodOnSuperClass();
} else if (a instanceof SubClass) {
a.methodOnSubClass();
}
}
would that work?
