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


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "overriding related problem" Watch "overriding related problem" New topic
Author

overriding related problem

pranav jog
Greenhorn

Joined: May 11, 2010
Posts: 1
Hi,

is it necessary that sub class must have same return type at the time of method overriding?
Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
From K&B Book

The return type must be the same as, or a subtype of, the return type declared
in the original overridden method in the superclass.


Bye,
Nicola
Raza Mohd
Ranch Hand

Joined: Jan 20, 2010
Posts: 247

hi Pranav,

yeah it is necessary that the return type of the overriding method should be same.
however, the access modifier can be different.

keep in mind:: We can not make the access modifier more restricted in the subclass.
eq. A overridden method cant be declared as private, if it is public in Super class.

restriction level for access modifier::::

public < protected < default<private


Good luck!!
A small leak can sink a Gigantic ship.>
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3826

Raza Mohd wrote:yeah it is necessary that the return type of the overriding method should be same.

Not necessarily, read the above quote from the K&B.
And Welcome to JavaRanch, Pranav


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Raza Mohd
Ranch Hand

Joined: Jan 20, 2010
Posts: 247

yes! i missread vijitha..
type may be a subtype of the overridden method in superclass.

like Arraylist is a subtype of List.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19674
    
  18

Keep in mind that this feature (called covariant returns) is added in Java 5.0. In Java 1.4 and before Raza's statement is correct - the return types must be the same.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Raza Mohd
Ranch Hand

Joined: Jan 20, 2010
Posts: 247

thanks for the confirmation Rob!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38466
    
  23
Welcome to the Ranch

You really ought to be looking, not at the K&B book, but at the Java™ Language Specification, which is the official documentation. That confirms that K&B are in fact correct, but there is a subtle difference: you can understand K&B

I am not sure what
R1 = | R2 |
means.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Absolute value :D

(More likely it means this, which I guess is sort of like that.)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38466
    
  23
Thank you, David. You are right: absolute value is obviously correct. Or is it "obvious value is absolutely correct"?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Absolutely :)

(Some day I'm going to re-read the JLS now that I'm actually starting to know Java...)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: overriding related problem