This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Casting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Casting" Watch "Casting" New topic
Author

Casting

Sam Cooper
Greenhorn

Joined: Feb 21, 2002
Posts: 14
This is question form MarcusGreen's Mock exam 2.
interface IFace{}
class CFace implements IFace{}
class Base{}
public class ObRef extends Base{
public static void main(String argv[]){
ObRef ob = new ObRef();
Base b = new Base();
Object o1 = new Object();
IFace o2 = new CFace();
}
}
1)o1=o2;
2)b=ob;
3)ob=b;
4)o1=b;

My question is under what circumstances the casting from an Interface to Object is possible and vice versa. Any thoughts are greatly appreciated.
Thanks
Vanitha Sugumaran
Ranch Hand

Joined: Apr 11, 2001
Posts: 356
I guess the question is about one of these causing compiler error.
o1=o2;
//works fine since o1 is type of Object
b=ob;
//Assign subclass to super class fine
ob=b;
//Error, can't assign super class to sub class
//cast needed
o1=b;
//fine


My question is under what circumstances the casting from an Interface to Object is possible and vice versa. Any thoughts are greatly appreciated.

You may want to take a look at this JLS section
http://java.sun.com/docs/books/jls/second_edition/html/conversions.doc.html#20232
Hope this helps,
Vanitha.
 
 
subject: Casting