Win a copy of Microservices Testing (Live Project) this week in the Spring 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

"Covariant Returns"

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Given a code example, determine if a method is correctly overriding or overloading
another method, and identify legal return values (including 'covariant returns'), for the
method."

That is the third statement in the first objective of SCJP 5.0.
Can someone evplain what 'covariant returns' means.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://dev2dev.bea.com/pub/a/2005/09/java_5_features.html?page=2

The above link references some of the Java 5 changes, including Covariant Returns
... Rich
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A covariant return is a return value that is a subtype of the return type of the method that's been inherited.

Example:
If class Honda inherits from class Car and Honda has a method called go()that has a Car return type. Then Honda's inherited go()can be overridden to return Honda types since Honda IS-A Car.

See p123 in K&B
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
covariant return types refines the return type of a method when overriding
it.

For example, the following declarations are legal in j2sdk 1.5 although they were illegal in prior versions of the Java programming language:



Here copy() method of subclass can return C (normal case) or subclass of C like D (only in JDK 5.0 called covariant return type). [Source: JLS Third Edition]

You can find lots of thread in this forum on the same topic.

Here is one thread...

confusion in code regarding inheritance according to java5


Naseem
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic