Do you mean using a Number object where an Object is required (just an example)?
All classes in Java can be used wherever one of their super-classes is specifically mentioned. Since sub-classes extend a super-class, they have all the properties the super-class has (and then some). The sub-class is not actually converted, though, its additional properties are just ignored in that moment.
But that's such a fundamental (and basic) concept in Java that I am wondering if I misunderstand what you're asking?
Casting doesn't change the type of an object, just the type of the reference. After the casting, the ClassB1 object will still be a ClassB1 object - it's just referenced by a ClassA1 reference variable.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
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.
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.