Java OCA OCP Practice Question 1505

Question

Which of the following is a legal return type of a method overriding the given method:

public Object  myMethod ()  {...} 

Select the best option. Select 1 option.

  • A. Object
  • B. String
  • C. Return type can be any object since all objects can be cast to Object.
  • D. void
  • E. None of the above.


Correct Option is  : C

Note

Since the original method is returning Object, the Overriding method can return any object type because all classes in Java ultimately extend from Object.

Since 1.5, Java allows covariant return types, which means an overriding method can have its return type as any subclass of the original return type of the overridden method.




PreviousNext

Related