473,763 Members | 9,161 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

overriding methods with a different return type

so i have a situation that i know cannot be solved directly, and i've
already worked around it, but i was hoping to find an explanation as to
why this behavior is as it is... i have an abstract class:

public abstract class Function
{
public abstract Number evaluate(Number parameter);
}

and i have a descendant of that class:

public class IntegerFunction extends Function
{
public Integer evaluate(Number parameter)
{
/* insert code here */
}
}

but this doesn't compile. i cannot override the superclass evaluate()
with the subclass evaluate() becuase of the different return types... even
though Integer is a Number.

my problem is this... i cannot envision a bad situation happening if this
were allowed.

let's say i write a program using the abstract class to keep it portable
and general:

Function f = Class.forName(I ntegerFunction) .newInstance();
Number x = new Integer(10);
Number y = f(x);

in my mind, i believe this works great. why? because i can change the
function f to say a different function that still guarantees its return
value is some form of Number, and it makes sense.

but, at the same time, some programs are better written to take direct
advantage of a specific return type, so i'd like to keep IntegerFunction 's
evaluate method returning type Integer only.

it seems like the policy that doesn't allow me to compile these two
classes as they are doesn't help prevent any problems, and instead limits
the programmer a bit.

can someone shed some light as to why this is the way it is? (in
particular, maybe give an easy example that can illustrate a problem that
can occur if my code was allowed to be compiled correctly...)

thanks!

murat

--
Murat Tasan
mx**@po.cwru.ed u
ta***@eecs.cwru .edu
mu*********@cwr u.edu
http://genomics.cwru.edu

Jul 17 '05 #1
5 8696
Murat Tasan wrote:
so i have a situation that i know cannot be solved directly, and i've
already worked around it, but i was hoping to find an explanation as to
why this behavior is as it is... i have an abstract class:

public abstract class Function
{
public abstract Number evaluate(Number parameter);
}

and i have a descendant of that class:

public class IntegerFunction extends Function
{
public Integer evaluate(Number parameter)
{
/* insert code here */
}
}

but this doesn't compile. i cannot override the superclass evaluate()
with the subclass evaluate() becuase of the different return types... even
though Integer is a Number.

my problem is this... i cannot envision a bad situation happening if this
were allowed.

let's say i write a program using the abstract class to keep it portable
and general:

Function f = Class.forName(I ntegerFunction) .newInstance();
Number x = new Integer(10);
Number y = f(x);

in my mind, i believe this works great. why? because i can change the
function f to say a different function that still guarantees its return
value is some form of Number, and it makes sense.

but, at the same time, some programs are better written to take direct
advantage of a specific return type, so i'd like to keep IntegerFunction 's
evaluate method returning type Integer only.

it seems like the policy that doesn't allow me to compile these two
classes as they are doesn't help prevent any problems, and instead limits
the programmer a bit.

can someone shed some light as to why this is the way it is? (in
particular, maybe give an easy example that can illustrate a problem that
can occur if my code was allowed to be compiled correctly...)

thanks!

murat

an Integer is a Number, you're right about that. But you can't change
the return type. You _can_ still return an Integer, but you'll have to
cast it to an Integer if you really want to use it as an Integer.
Using your example:

public abstract class Function
{
public abstract Number evaluate(Number parameter);
}
public class IntegerFunction extends Function
{
public Number evaluate(Number parameter)
{
/* insert code here */
Integer i = new Integer(0);
return i;
}
}
Function f = Class.forName(I ntegerFunction) .newInstance();
Number x = new Integer(10);
Number y = f.evalate(x);

Nothing changes (ie this is what you said you wanted to do)... you just
have to fix your return type in IntegerFunction .

However this won't compile,
Function f = Class.forName(I ntegerFunction) .newInstance();
Number x = new Integer(10);
Integer y = f.evalate(x);

but you can do this:

Function f = Class.forName(I ntegerFunction) .newInstance();
Number x = new Integer(10);
Number y = f.evalate(x);
if(y instanceof Integer) {
Integer z = (Integer)y;
}
But really, this is almost a moot point because Number has intValue(),
floatValue(), longValue(), doubleValue(), byteValue(), shortValue() in
it anyway.

-Bryan

Jul 17 '05 #2
yeah, that is basically what i've done, but it still doesn't make sense to
me.

if i'm a programmer and i want to use the abstract class, i'll receive a
Number as the return value, but if i want to use a specific subclass, i
should be able to be more specific about what return value i receive, and
that subclass should basically define it's return type for me: Integer.

p.s. i know about the typeValue() methods of Number, but the general
problem lingers in many other situations.

it just kinda seems like a design oversight in the java language, as i
still cannot envision a problem occuring from what i'd like to do.

oh well... maybe i should look at c#...

murat

On Thu, 13 Nov 2003, Bryan Boone wrote:
Murat Tasan wrote:
so i have a situation that i know cannot be solved directly, and i've
already worked around it, but i was hoping to find an explanation as to
why this behavior is as it is... i have an abstract class:

public abstract class Function
{
public abstract Number evaluate(Number parameter);
}

and i have a descendant of that class:

public class IntegerFunction extends Function
{
public Integer evaluate(Number parameter)
{
/* insert code here */
}
}

but this doesn't compile. i cannot override the superclass evaluate()
with the subclass evaluate() becuase of the different return types... even
though Integer is a Number.

my problem is this... i cannot envision a bad situation happening if this
were allowed.

let's say i write a program using the abstract class to keep it portable
and general:

Function f = Class.forName(I ntegerFunction) .newInstance();
Number x = new Integer(10);
Number y = f(x);

in my mind, i believe this works great. why? because i can change the
function f to say a different function that still guarantees its return
value is some form of Number, and it makes sense.

but, at the same time, some programs are better written to take direct
advantage of a specific return type, so i'd like to keep IntegerFunction 's
evaluate method returning type Integer only.

it seems like the policy that doesn't allow me to compile these two
classes as they are doesn't help prevent any problems, and instead limits
the programmer a bit.

can someone shed some light as to why this is the way it is? (in
particular, maybe give an easy example that can illustrate a problem that
can occur if my code was allowed to be compiled correctly...)

thanks!

murat

an Integer is a Number, you're right about that. But you can't change
the return type. You _can_ still return an Integer, but you'll have to
cast it to an Integer if you really want to use it as an Integer.
Using your example:

public abstract class Function
{
public abstract Number evaluate(Number parameter);
}
public class IntegerFunction extends Function
{
public Number evaluate(Number parameter)
{
/* insert code here */
Integer i = new Integer(0);
return i;
}
}
Function f = Class.forName(I ntegerFunction) .newInstance();
Number x = new Integer(10);
Number y = f.evalate(x);

Nothing changes (ie this is what you said you wanted to do)... you just
have to fix your return type in IntegerFunction .

However this won't compile,
Function f = Class.forName(I ntegerFunction) .newInstance();
Number x = new Integer(10);
Integer y = f.evalate(x);

but you can do this:

Function f = Class.forName(I ntegerFunction) .newInstance();
Number x = new Integer(10);
Number y = f.evalate(x);
if(y instanceof Integer) {
Integer z = (Integer)y;
}
But really, this is almost a moot point because Number has intValue(),
floatValue(), longValue(), doubleValue(), byteValue(), shortValue() in
it anyway.

-Bryan


--
Murat Tasan
mx**@po.cwru.ed u
ta***@eecs.cwru .edu
mu*********@cwr u.edu
http://genomics.cwru.edu

Jul 17 '05 #3
Read up on java 1.5. What you describe is usually referred to as "covariant
return types" and it is supported in 1.5 along with all kinds of other
extensions of the type system.

Silvio Bierman
Jul 17 '05 #4
jb
Why not return an interface, and have the return type implement them?

Murat Tasan <ta***@eecs.cwr u.edu> wrote in message news:<Pine.SOL. 4.53.0311122243 580.2354@homer> ...
so i have a situation that i know cannot be solved directly, and i've
already worked around it, but i was hoping to find an explanation as to
why this behavior is as it is... i have an abstract class:

public abstract class Function
{
public abstract Number evaluate(Number parameter);
}

and i have a descendant of that class:

public class IntegerFunction extends Function
{
public Integer evaluate(Number parameter)
{
/* insert code here */
}
}

but this doesn't compile. i cannot override the superclass evaluate()
with the subclass evaluate() becuase of the different return types... even
though Integer is a Number.

my problem is this... i cannot envision a bad situation happening if this
were allowed.

let's say i write a program using the abstract class to keep it portable
and general:

Function f = Class.forName(I ntegerFunction) .newInstance();
Number x = new Integer(10);
Number y = f(x);

in my mind, i believe this works great. why? because i can change the
function f to say a different function that still guarantees its return
value is some form of Number, and it makes sense.

but, at the same time, some programs are better written to take direct
advantage of a specific return type, so i'd like to keep IntegerFunction 's
evaluate method returning type Integer only.

it seems like the policy that doesn't allow me to compile these two
classes as they are doesn't help prevent any problems, and instead limits
the programmer a bit.

can someone shed some light as to why this is the way it is? (in
particular, maybe give an easy example that can illustrate a problem that
can occur if my code was allowed to be compiled correctly...)

thanks!

murat

Jul 17 '05 #5
so i have a situation that i know cannot be solved directly, and i've
already worked around it, but i was hoping to find an explanation as to
why this behavior is as it is... i have an abstract class:

public abstract class Function
{
public abstract Number evaluate(Number parameter);
}

and i have a descendant of that class:

public class IntegerFunction extends Function
{
public Integer evaluate(Number parameter)
{
/* insert code here */
}
}

but this doesn't compile. i cannot override the superclass evaluate()
with the subclass evaluate() becuase of the different return types... even
though Integer is a Number.

my problem is this... i cannot envision a bad situation happening if this
were allowed.


And you are right. It would be safe to allow overriding methods to
return a more precise type. I think it's just Sun being conservative on
this issue.

BTW, it looks like you want a Java-like language that supports first
class functions, so you should really try Nice: http://nice.sf.net :-)
Then you could write Number->Number and Number->int, and you could use a
value of type Number->int where Number->Number is expected.

Cheers,

Daniel

Jul 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
4198
by: Ali Eghtebas | last post by:
Hi, I have 3 questions regarding the code below: 1) Why can't I trap the KEYDOWN while I can trap KEYUP? 2) Is it correct that I use Return True within the IF-Statement? (I've already read the documentation but it is rather hard to understand so please don't refer to it :) 3) Many examples in the newsgroups use Return MyBase.ProcessKeyPreview(m) as the last code line while I have used Return MyBase.ProcessKeyEventArgs(m)
15
24010
by: Susan Baker | last post by:
Hello everybody, I'm new to C++ (I have some C background). I've read up on this topic a few times but it just dosen't seem to be sinking in. 1. Whats the difference between overloading and overriding? 2. When is one preferable to use as opposed to the other? 3. How are virtual functions related to this topic (overloading/overriding) - a real world example of using virtual functions would be very much appreciated.
3
4802
by: news.microsoft.com | last post by:
Hi, It is possible to override a non virtual method with the "new" keyword So how is this different from specifying a method as virtual then providing the override keyword? Is there any differences between these two methods of overriding? Thanks.
4
1933
by: ORi | last post by:
Hi all ! There's a question I've been bothering for a while: I'm actually developing architectural frameworks for application developing and I think virtual methods, although needed because of the flexibility they introduce (flexibility really needed in framework developing), are often a nuisance for final developers. They don't like them because they never know if base class must be called and where should they place the call if...
17
2917
by: Bob Weiner | last post by:
What is the purpose of hiding intead of overriding a method? I have googled the question but haven't found anything that makes any sense of it. In the code below, the only difference is that when the Poodle is upcast to the Dog (in its wildest dreams) it then says "bow wow" where the bernard always says "woof" (see code). Basically, it appears that I'm hiding the poodle's speak method from everything except the poodle. Why would I...
18
4744
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the class where only the "searched" property has a value. I expected to get the index into the arraylist where I could then get the entire class instance. However, the 'indexof' is never calling my overloaded, overrides Equals method. Here is the...
8
3748
by: Kenneth Baltrinic | last post by:
When one overrides the Equals() method of an object, one is supposed to override GetHashCode() as well and this makes good sense. But I have seen lots of people who do this and do not override the == and != opperators. Am I missing something or when would one want to have different implementations for Equals and ==? --Ken
10
105197
by: r035198x | last post by:
The Object class has five non final methods namely equals, hashCode, toString, clone, and finalize. These were designed to be overridden according to specific general contracts. Other classes that make use of these methods assume that the methods obey these contracts so it is necessary to ensure that if your classes override these methods, they do so correctly. In this article I'll take a look at the equals and hashCode methods. ...
12
6136
by: Gordon | last post by:
I want to provide a set of static functions in a superclass that work with class constants defined in a decendant of that class. Unfortunately I've run into a snag with this idea. Example: class SuperClass { const CNST = 'Super class'; public static function getCnst () {
0
9387
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10002
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9823
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8822
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7368
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6643
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3528
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2794
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.