• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Dan's Reference Conversion Doubt

 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Simple explaination:

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

but

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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic