Originally posted by Mathew Chen:
Hi,
I am kinda confused about upcasting in Java?I thought converting a subclass to a super class is not possible.
Pls advice.
Thanks
Mathew Chen
First off there is no "conversion" in Java per se. You are simply changing the variable type that references the same object. Second, I believe you have the concept upside down. You can always reference a sub-type using a super-type reference:
Object foo = new String("Billy Bob Jolie");
This is upcasting and it's risky:
String bar = (String)foo;
If you are just kidding about foo being a String your
thread will crash.