Java OCA OCP Practice Question 298

Question

Given the following:

public class Food { } 
public class Fruit extends Food { } 
public class Shape extends Fruit { } 

public class Rectangle extends Shape { } 

public class MyClass { 
     public Fruit feedMe() { return new Fruit(); } 
} 

public class MySubClass extends MyClass { 
     public ????? feedMe() { return new Rectangle (); } 
} 

Which of the following are legal return types for feedMe() in class MySubClass?

  • A. Object
  • B. Food
  • C. Fruit
  • D. Shape
  • E. Rectangle


C, D, E.

Note

Covariant returns are legal in 5.0, so in addition to the exact return type of the superclass' version, any subclass of that type is also legal.




PreviousNext

Related