Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Covariant return types does not apply to primitive types (Java OCA 8 Programmer I Study Guide)

 
Suresh Regmi
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rules for overriding method (chapter 5 page 248)

#4. This co-variant rule does not apply for primitive types(it can have different return type on parent and child class). It applies to non-primitive types.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what's your question, problem or issue?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:And what's your question, problem or issue?

Oh wait, I think I discovered it myself

The original 4th rule of overriding states "If the method returns a value, it must be the same or a subclass of the method in the parent class, known as covariant return types". And you want to rephrase this rule to make it more obvious ithat the covariant return type only applies to objects (classes) and not to primitive data types. Correct?
 
Suresh Regmi
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Roel,, just to Rephrase it with the exception for primitive data types.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suresh Regmi wrote:Yes Roel,, just to Rephrase it with the exception for primitive data types.

In my opinion it's not really necessary, because it's already in the statement, maybe not that obvious.

The rule clearly states that the return type must be the same or a subclass. And as you know primitive data types are not classes, so subclasses only apply for objects (non primitive data types). So for primitives it must be the same.
 
Suresh Regmi
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
New Learners like me will be confused. It's better to rephrase it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic