• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

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
 
I'm not dead! I feel happy! I'd like to go for a walk! I'll even read a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic