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.
Wednesday, May 14, 2008
When do we need explicit casting for the object assignment?
Related Articles...
Trivia
- what does load-on-startup element mean in web.xml?
- What is the difference between VARCHAR and VARCHAR2?
- Why Prepared Statements are faster? Are they compiled?
- Difference between Client-side & Server-side DB cursors?
- How to invoke an Applet packaged as a JAR?
- Class variable, Member variable, and Local variable
Subscribe to:
Post Comments (Atom)
1 comment:
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
Post a Comment