If a method takes an Object parameter, can this parameter be type casted by
any class?
Example:
public int compareTo(Object o) {
Name n = (Name) o;
}
public int MultiplyPrint(Object o) {
String testString = (String) o;
Int num = Integer.parseInt(testString);
int theResult = Math.pow(num,2);
return theResult;
}
public Orange Apple2Orange(Object o) {
Apple apple = (Apple) o;
// Convert to Orange
Orange orange = (Orange) apple;
return Orange;
}
Also, is it poor programming style to rely on type casting?
TIA
James Davis
Dan Nuttle - 12 Mar 2005 03:54 GMT
You can TRY to cast an Object to another type, but of course, if the Object
isn't an instance of that type, you'll get a ClassCastException. For
instance, if the Object is actually a String, and you try to cast it as an
OutputStream, it won't work.
Whether or not it's poor programming to cast a type all depends on the
situation. I don't think there's a single simple answer to that question.
> If a method takes an Object parameter, can this parameter be type casted by
> any class?
[quoted text clipped - 23 lines]
>
> James Davis