File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Dan's Reference Conversion Doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Dan Watch "Dan New topic

Dan's Reference Conversion Doubt

Jas Oberai
Ranch Hand

Joined: Jan 16, 2005
Posts: 231
Hi Friends,
This question is from Dan's Questionaire,i am not able to understand the
wordings,can you please tell me what the author means to convey here,and
how can the error be removed from this code.Thanks.

[ May 12, 2005: Message edited by: jas oberai ]

SCJP 1.4 (88%)<br />SCWCD 1.4 (88%)
Rohan Kayan
Ranch Hand

Joined: Sep 17, 2004
Posts: 123

interface I1 {}
interface I2 {}
class Base implements I1 {}
class Sub extends Base implements I2 {}
class Silver
public static void main(String []args)
Base[] base = {new Base()};
Sub sub[] = new Sub[1]; // 1
Object obj = base; // 2
sub = (Sub[])obj; // 3
I1 []i1 = (I1[])obj; // 4

Actually at the compile time it will allow , as we are implicit downcasting.
But at the run time it will check for the actual object in this case the actual object refered by obj is of Base type that's why it will give error . To remove the change

Object obj = sub; // 2

SCWCD 1.4, SCJP 1.4
Timmy Marks
Ranch Hand

Joined: Dec 01, 2003
Posts: 226
Simple explaination:

All Women are People, so if you have a Woman you know she is a Person


All People are not Women, so if you have a Person you cannot "cast" is as a Woman

now substitute Base for People and Sub for Women and you can see why it is a mistake.
I agree. Here's the link:
subject: Dan's Reference Conversion Doubt
It's not a secret anymore!