Java OCA OCP Practice Question 1403

Question

Given the following definitions and reference declarations:

interface Interface1  {  } 
interface Interface2  {  } 
class MyClass1 implements Interface1  {  } 
class MyClass2 implements Interface2  {  } 
class MyClass3 extends MyClass1 implements Interface2  {  } 
MyClass1 o1; 
MyClass2 o2; 
MyClass3 o3; 

Which of these statements are legal?

Select 3 options

  • A. class MyClass4 extends MyClass3 implements Interface1, Interface2 { }
  • B. o3 = o1;
  • C. o3 = o2;
  • D. Interface1 i1 = o3; Interface2 i2 = (Interface2) i1;
  • E. Interface1 b = o3;


Correct Options are  : A D E

Note

For Option A.

The implements Interface1, Interface2 is redundant here because MyClass3 already implements Interface1 and Interface2, it is not invalid.

For Option B.

superclass reference cannot be assigned to subclass reference without explicit cast.

For Option C.

There is no way a reference of class MyClass2 (which is o2) can point to an object of class MyClass3 because MyClass2 and MyClass3 have no inheritance relationship.

So this assignment is rejected at compile time itself.

For Option D.

This is valid because at run time i1 actually refers to an object that implements Interface2.

For Option E.

Because MyClass3 extends MyClass1 which implements Interface1.




PreviousNext

Related