Java OCA OCP Practice Question 699

Question

Given the following class definitions :

interface MyInterface{}; 
class A  {}; 
class B extends A implements MyInterface{}; 
class C implements MyInterface{}; 

and the following object instantiations:

A a = new A (); 
B b = new B (); 
C c = new C (); 

Which of the following assignments are legal at compile time?

Select 1 option

  • A. b = c;
  • B. c = b;
  • C. MyInterface i = c;
  • D. c = (C) b;
  • E. b = a;


Correct Option is  : C

Note

The statements c = b and b = c are illegal, since neither of the classes C and B is a subclass of the other.

Even though a cast is provided, the statement c = (C) b is illegal because the object referred to by b cannot ever be of type C.




PreviousNext

Related