Java OCA OCP Practice Question 1280

Question

Given that Short and Integer extend Number,

what type can be used to fill in the blank in the class below to allow it to compile?

package mypkg; //from  w  w w  . ja  v  a2 s .  c  o  m
? 
interface Movie { public Integer play(); } 

abstract class Car { public Short play() {return 3;} } 

public final class ElectrCar extends Car implements Movie { 
   public ___ play() { 
      return null; 
   } 
} 
  • A. Integer
  • B. Short
  • C. Number
  • D. None of the above


D.

Note

The play() method is overridden in ElectrCar for both Movie and Car, so the return type must be covariant with both.

Unfortunately, the inherited methods must also be compatible with each other.

Since Integer is not a subclass of Short, and vice versa, there is no subclass that can be used to fill in the blank that would allow the code to compile.

In other words, the ElectrCar class cannot compile regardless of its implementation of play(), making Option D the correct answer.




PreviousNext

Related