Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is this explanation correct?

 
Jay Taylor
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is taken from a practice test from uCertify application.

I cannot follow the logic and was feeling confused so I decided to seek assistance. Is the explanation provided accurate and if so.. How?


QUESTION:
interface A1{}
interface A2 {}
class C1 implements A1 {}
class C2 implements A2 {}
class C3 extends C1 implements A2 {}
C1 object1;
C2 object2;
C3 object3;

Which of the following statements is legal in the context of the above code?
A� object3=object1;
B- object2=object1;
C- object3=object2;
D- A2 c = object1;
E- A1 a=object2;
F� A1 b=object3;

Explanation: Answer option F is correct.
The A1 b = object3; statement in legal, in which b is the reference variable of the interface A1, which is assigned to hold object3. Here, object3 is the reference variable of the class C3, which implements the interface A2 and inherits the class C1. The class C1 implements the A1 interface. Therefore, the class C3 also indirectly implements A1.
 
Jesus Angeles
Ranch Hand
Posts: 2068
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It tests your knowledge on polymorphism and inheritance.

You can think it this way.

I just replaced them with their class type.

A� C3=C1; Is C1 a C3? NO
B- C2=C1; Is C1 a C2? NO
C- C3=C2; Is C2 a C3? NO
D- A2=C1; Is C1 a A2? NO
E- A1=C2; Is C2 a A1? NO
F� A1=C3; Is C3 a A1? YES
 
Kaydell Leavitt
Ranch Hand
Posts: 689
Eclipse IDE Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question is complex enough that I would draw a UML diagram to figure it out.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic