Ninad Kulkarni wrote:Hi Aakash,
Up cast means casting from sub class to super class
Down cast means casting from super class to sub class
Up casting happen automatically or you can do it manually.
Down casting happen only manually. You have to do it.
See comment at end of each line
Correct me if I am wrong
aabir sanyal wrote:
This will throw runtime ClassCastExceptions right??
jeetendra Choudhary wrote:No there is no ClassCastException. It Compiles and run fine. I have a confusion that Min is an interface we are supposed not to create object of interface.
so can any one explain me What this Line is doing here..??
i think This Link would be helpful.
Thanks & Regards
avi sinha wrote:
what i know is:
1> you can cast object refernces only if they are in the same inheritance tree. you can ,means it will compile .it doesn't say that it will not give any runtime error.
2> if the original object & the referance are of the same type ,it will run fine too.
3>if the original object is an object of the subclass of the referance type ,it will run fine too.
Ninad Kulkarni wrote:@ Aakash,
Object o = new Obejct();
String s = new String("String");
Integer i = new Integer(1);
o = s; It will compile and runs fine. Automatic upcast.
s = (String)o; It will compile and runs fine. Manual downcast needed.
Nitish Bangera wrote:
This will compile fine but execute only if m has the object of B or C.
This will compile fine but execute only if o has an Integer Object
Both cases, compilation is successful but there is a possibility of choking at runtime.