Wednesday, May 14, 2008

When do we need explicit casting for the object assignment?


While assigning an object of data type ‘Superclass’ to an object of the data type ‘Subclass’, explicit casting is required. If we do the reverse, then the casting is done automatically.

Object objRef = new CustomeClass();
CustomeClass customeClassRef = new CustomeClass();


customeClassRef = (CustomClass) objRef; //explict casting required
objRef = customClassRef; //explict casting NOT required


Note: Since, ‘Object’ is a cosmic superclass of all the class in Java, so it’ll automatically be a superclas of ‘CustomClass’ as well.



Share/Save/Bookmark


1 comment:

dissertation writing said...

I have been visiting various blogs for my dissertation research. I have found your blog to be quite useful. Keep updating your blog with valuable information... Regards