• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

overriding related problem

 
pranav jog
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

is it necessary that sub class must have same return type at the time of method overriding?
 
Nicola Garofalo
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Raza Mohd
Ranch Hand
Posts: 247
Java MyEclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Raza Mohd
Ranch Hand
Posts: 247
Java MyEclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20559
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Raza Mohd
Ranch Hand
Posts: 247
Java MyEclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the confirmation Rob!
 
Campbell Ritchie
Sheriff
Pie
Posts: 49472
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Absolute value :D

(More likely it means this, which I guess is sort of like that.)
 
Campbell Ritchie
Sheriff
Pie
Posts: 49472
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, David. You are right: absolute value is obviously correct. Or is it "obvious value is absolutely correct"?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Absolutely :)

(Some day I'm going to re-read the JLS now that I'm actually starting to know Java...)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic