Win a copy of Getting started with Java on the Raspberry Pi this week in the Raspberry Pi forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Liutauras Vilda
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Piet Souris

Sybe:Java OCA/OCP Practice Tests Chapter 7 question 28 mock answer typo

 
Ranch Hand
Posts: 215
11
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sybe:Java OCA/OCP Practice Tests Chapter 7 question 28 mock answer typo

overridden methods share the exact same name, list of parameters, and return type.

return type should be removed because it is covariant.

by alinvlad05
 
Bartender
Posts: 1737
63
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since Java 5 it is hard to make a very short, yet still true statement about the return type for Overridden methods.

For primitive return types it must still match precisely, if I recall correctly.

For reference return types, it may either match precisely or be any known sub-class (the definition of Covariance).

The notion of covariance does not apply to primitive return types in any way:
jshell> class Base { int method() { return 15;} }
|  modified class Base

jshell> class Derived extends Base { long method() { return 77;}}
|  Error:
|  method() in Derived cannot override method() in Base
|    return type long is not compatible with int
|  class Derived extends Base { long method() { return 77;}}
|                               ^-------------------------^

jshell> class Derived extends Base { short method() { return Short.valueOf("12");}}
|  Error:
|  method() in Derived cannot override method() in Base
|    return type short is not compatible with int
|  class Derived extends Base { short method() { return Short.valueOf("12");}}
|                               ^-------------------------------------------^


But this of course, as you say, is fine:
jshell> class BaseRefRet { Number method() { return Integer.valueOf(15);} }
|  created class BaseRefRet

jshell> class DerivedRefRet extends BaseRefRet { @Override Double method() { return Double.valueOf(3.14159);}}
|  created class DerivedRefRet


If I was forced to make a one-sentence statement that included the words "return type" I would be unhappy, but would probably say:

overridden methods share the exact same name, list of parameters, and compatible return type.


Where the meaning of "compatible" would be the somewhat complex stuff shown above.

The question might have been a hold-over from Very Old Java books (it was true as of the Java 1.4 release) or was possibly just a careless statement made in haste.

 
author & internet detective
Posts: 41763
885
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jesse: We started writing books with Java 8 so no such excuse.

Vlad: I've added this to the errata. I also checked and we didn't use this question in the Java 11 book
 
It's exactly the same and completely different as this tiny ad:
Low Tech Laboratory
https://www.kickstarter.com/projects/paulwheaton/low-tech-0
reply
    Bookmark Topic Watch Topic
  • New Topic