Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to override a method with different return type

Reply
Thread Tools

How to override a method with different return type

 
 
sandy
Guest
Posts: n/a
 
      11-15-2005
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

 
Reply With Quote
 
 
 
 
J. Verdrengh
Guest
Posts: n/a
 
      11-15-2005
> 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.


 
Reply With Quote
 
 
 
 
sks
Guest
Posts: n/a
 
      11-15-2005

"J. Verdrengh" <une-> wrote in message
news:4379dbf8$0$3637$...
>> 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.


You can do that with Object types (not primitives) in 1.5


 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      11-15-2005
On Tue, 15 Nov 2005 13:21:01 -0000, "sks" <> wrote,
quoted or indirectly quoted someone who said :

>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.
..
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Avoid derived class to override a method yet should allow callers toinvoke the method Ethan Java 4 09-07-2011 03:14 PM
Override method name and original method access Donn Ingle Python 0 11-12-2007 07:41 PM
implementing method with different return type Pradeep Kumar Java 29 04-15-2006 03:39 AM
override virtual, covariant return type David White C++ 12 03-31-2005 11:42 PM
overriding method with different return type Paul J. Lucas Java 5 04-12-2004 09:45 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57