• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Interfaces?

 
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question :Given the following definitions and refernce 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;
Options :1)class C4 extends C3 implements I1,I2{}
2) o3=o1;
3) o3=o2;
4) I1 i1 = o3;I2 i2=(I2)i1;
5) I1 b = o3;
Answer is : 1,4,5
Can any one please explain me the above concepts..
 
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1 is obviously correct. as C4 is some unrelated class and they are declaring its inheritance relationships etc.
2 C3 is a subclass of C1, so o1=o3; would be ok. (if you do o3=o1 you need a cast like o3=(C3)o1; so NOT OK
3 o3=o2;NOT OK as you need a cast here again
4 I1 i1 = o3; C3 implements I1 (actually implicitly , as C3 extends C1, C1 implements I1 ,hence implictly C3 also implements I1) so OK
I2 i2=(I2)i1; since casting given it's fine
5 I1 b = o3; (as we said above C3 implements I1) so OK.
concepts
class Test implements interface II
class Hello extends Test
references of II can denote objects of the class that implement this interface.
so II obj = new Test();
and since Hello is a subclass of Test so
II obj1 = new Hello(); (as class Hello also implictly implements interface II)

Originally posted by sonir shah:
Question :Given the following definitions and refernce 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;
Options :1)class C4 extends C3 implements I1,I2{}
2) o3=o1;
3) o3=o2;
4) I1 i1 = o3;I2 i2=(I2)i1;
5) I1 b = o3;
Answer is : 1,4,5
Can any one please explain me the above concepts..

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A small correction to the previous answer.

I2 i2=(I2)i1; since casting given it's fine


A cast is also checked during compile time. If the classes dont have any common subclas then it is a compile type error. However in the above case since I1 and I2 have a common subclass in C3, the cast is allowed.
Take a look at this example. This will result in a compile time error.


Hope this helps
reply
    Bookmark Topic Watch Topic
  • New Topic