File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Covariance, Method Overriding and Return Type Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Covariance, Method Overriding and Return Type" Watch "Covariance, Method Overriding and Return Type" New topic
Author

Covariance, Method Overriding and Return Type

Harshil Mehta
Ranch Hand

Joined: Mar 17, 2005
Posts: 64
hello ranchers,

in java 1.5, in case of overriding a method, return type of the method can be substituted by subtype of it.

in the example follow:


with two new classes with above classes as:
is a legal usage of covariance of return type of method getACar() as type is narrowed down to subclass Maruti from original return type Car.

Can it be explained that why follwing code is illegal?

Here return type is promoted to superclass of original type Car. So it's example of contravariance.

I just want to know what's wrong in this "practically"?
Harshil Mehta
Ranch Hand

Joined: Mar 17, 2005
Posts: 64
Sorry, forgot to add "extends Distributor" in definition of class Dealer.

Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
It violates Liskov's Substitution Principle: code that works with a class should also work with all of its subclasses, without knowing about them.

In your example, code working on a Distributor could expect to get a Car when calling the method, and therefore would break if subclasses would be allowed to have less specific return types.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Harshil Mehta
Ranch Hand

Joined: Mar 17, 2005
Posts: 64
Thank you very much Ilja.

I got what you want to say. However in order to achieve what i want, i.e. allowing supertype 'Vehicle' to be returned, the code working on 'Distributor' must cast the return type to 'Vehicle'.

It will lead to confusing desing i guess. Or is it completely wrong?
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Harshil Mehta:
However in order to achieve what i want, i.e. allowing supertype 'Vehicle' to be returned, the code working on 'Distributor' must cast the return type to 'Vehicle'.


Does it? Why?
Harshil Mehta
Ranch Hand

Joined: Mar 17, 2005
Posts: 64

quote:
--------------------------------------------------------------------------------
Originally posted by Harshil Mehta:
However in order to achieve what i want, i.e. allowing supertype 'Vehicle' to be returned, the code working on 'Distributor' must cast the return type to 'Vehicle'.

--------------------------------------------------------------------------------


Does it? Why?

It certainly does not. What i want to ask is if contravariance had been allowed, wouldn't the user class of 'Dealer' require casting the return type to appropriate subclass of 'Vehicle'?

I know i am asking question for a hypothetical situation. Consider this vis-a-vis code in above posts:



ant the user class of Dealer is say 'MarutiDealer' as:


So here return type of 'getACar()' is cast to Maruti!!

My original question remains same:

It will lead to confusing desing i guess. Or is it completely wrong?
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

Originally posted by Harshil Mehta:
Thank you very much Ilja.

I got what you want to say. However in order to achieve what i want, i.e. allowing supertype 'Vehicle' to be returned, the code working on 'Distributor' must cast the return type to 'Vehicle'.

It will lead to confusing desing i guess. Or is it completely wrong?


I'm confused already. That cast would be pointless. Better to ask why are you trying to do this and what you hope to achieve?
 
jQuery in Action, 2nd edition
 
subject: Covariance, Method Overriding and Return Type
 
Similar Threads
Inteface Sub Class
DOUBT on DOWNCASTING
Casting in java
private and protected
Overriding/overloading