• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Covariant return types

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone give me a example for covariant return types,also say what do you mean by Covariant return types.
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This should help -> http://download.oracle.com/docs/cd/E17409_01/javase/tutorial/java/javaOO/returnvalue.html
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's say super class is Animal and has a method with signature public Animal aMethod().

If Dog is subclass of animal then if the Dog class overrides the aMethod() ,the its return type can be Animal or Dog or whatever that is Subtype of Animal.
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
from java 1.5 while overriding, it is now possible to return the subtype of object you are calling.

for example as we know that String extends Object.
if the superclass method is returning the Object.
then its subclass method can return String object.......as string extends Object but vice versa is not true.....

check the below code

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to the Sierra/Bates SCJP 6 study guide (page 127), covariant returns were introduced in Java 5. But I've noticed that when compiling under Java 1.4, you can still return an object of the subclass type. So the only difference appears to be that you can declare the subtype's override to return an object of the subclass type.

In other words, using Phil Sohar's example, the following would not compile under 1.4, using javac -source 1.4 Covart.java:



However, by simply changing the declared return type (but not the actual return statement), it will indeed compile under 1.4. Notice that all I changed was the return type, from String to Object:



So this Java padawan humbly asks, why did they bother adding this to Java 5? Is it just to save a cast in the client code?
 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
More freedom with overriding, now you have.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you are right.................
 
You can't expect to wield supreme executive power just because
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic