Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2005

Tip: Looking for answers? Try searching our database.

How to override a method with different return type

Thread view: 
sandy - 15 Nov 2005 12:41 GMT
hi all,
    To begin with i thank the creaters of this group.
     I am new java world and facing a new challenge.
    i have a method in class A named
       protected int foo(int args) { /* ..... */ }
    and have a class B that extends class A and would like to have a
method foo in it which overrides it as
      public double foo(int args) { /*........*/}
i need something like above code, i.e the return type be different ,
can anybody help me on this

thanks
sandy
J. Verdrengh - 15 Nov 2005 13:00 GMT
>     i have a method in class A named
>        protected int foo(int args) { /* ..... */ }
>     and have a class B that extends class A and would like to have a
> method foo in it which overrides it as
>       public double foo(int args) { /*........*/}

This is not allowed in Java. And if it were allowed, the return type of the
overriding method (double in your case) should be 'more specific' then the
return type of the overriden method (int in your case). After all a subclass
should obey the contract of its superclass. So if the return type of the
overriden method is double, it would be legal (from a object-oriënted
programming point-of-view) to override it with a method returning a int
(since each int can be implicitly casted to a double). But again: none of
this is possible in Java.
sks - 15 Nov 2005 13:21 GMT
>>     i have a method in class A named
>>        protected int foo(int args) { /* ..... */ }
[quoted text clipped - 10 lines]
> returning a int (since each int can be implicitly casted to a double). But
> again: none of this is possible in Java.

You can do that with Object types (not primitives) in 1.5
Roedy Green - 15 Nov 2005 18:35 GMT
>You can do that with Object types (not primitives) in 1.5
and the overriding type has to be more specific, e.g. base returns
Dog,  override always returns Dalmatian, not the reverse.  Dalmatian
is still a Dog.

You can't break the contract of the original base class.
.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.