• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

help me for this question... (interface question)

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
pls look at the fallowing example,
Given the following definitions and reference declarations, which of these statements are legal?
interface I1 { }
interface I2 { }
class C1 implements I1 { }
class C2 implements I2 { }
class C3 extends C1 implements I2 { }
C1 o1;
C2 o2;
C3 o3;
Select 3 correct options
a class C4 extends C3 implements I1, I2 { }
b o3 = o1;
c o3 = o2;
d I1 i1 = o3; I2 i2 = (I2) i1;
e I1 b = o3;

Answers given r a,d,e.
But in d,the statement I2 i2 = (I2) i1; ,how it is possible as there is no relationship between I1 and I2 inteface.?
[ Jess gave this a bit more descriptive title ]
[ February 24, 2003: Message edited by: Jessica Sant ]
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Prashant,
Reference i1 refers to an object instance, 03, that is of type C3. Type C3 extends type C1 that implements I1.
In this cast, the cast only works because the object instance encountered at run-time just happens to implement I1. It is not a good idea to write code based on the assumption that you will get lucky at run-time.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At compile time the casting is allowed because the two interfaces do not have incompatible method declarations, regardless they are not related. You can read more on JLS 5.5
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic