Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #32045
| Newsgroups | comp.lang.java.programmer |
|---|---|
| Date | 2015-07-16 07:23 -0700 |
| References | <mo8a4l$h99$1@speranza.aioe.org> |
| Message-ID | <52bdf1e9-9839-407f-83d0-ba2e031f4826@googlegroups.com> (permalink) |
| Subject | Re: overriding with different return value type |
| From | e.d.programmer@gmail.com |
On Thursday, July 16, 2015 at 9:04:37 AM UTC-4, Xue Feng wrote:
> According to the JDK manual, overriding requirs that
> (1) the same signature
> (2) the same return value type
> The following example demonstrates it:
>
> class Base{
> public int sum(int x, int y){
> System.out.println("Int");
> return x + y;
> }
> }
>
> class Ext extends Base{
> public double sum(int x, int y){
> System.out.println("Double");
> return (double)(x + y);
> }
>
> }
>
> The overriding sum in Ext returns double, while the overrided sum in
> Base returns int. Their return value type are different. So, we get
> error message when compiling.
>
> But the other example seems work in different way. The Object class has
> a clone() method, and its header is like this:
> protected Object clone() throws CloneNotSupportedException
> The clone() in Object class returns an Object's instance. Suppose that
> there is a class implements cloneable interface:
>
> class Foobar implements Cloneable{
> public Foobar clone() throws CloneNotSupportedException{
> System.out.println("Foobar");
> return (Foobar)super.clone();
> }
> }
>
> The clone() in Foobar class returns an Foobar's instance. It's obvious
> that the two clone() methods have different return value type, but no
> error message comes while compiling. I wonder why the clone() method
> works in different way.
You can only override a method with the same return value, or an extension of that return value. Foobar extends Object by default. double accepts int values but does not extend int. Overloading an int with a double is the same as overloading an Integer with a Double. Integer extends Number which extends Object. Double extends Number which extends Object. Double does not extend Integer.
When you override an Object with a Foobar you're saying implicit conversion is allowed:
Foobar f = new Foobar();
Object o = f;
You can't do that with numbers. You're trying to override an int with a double.
Double d = 1d;
Integer i = d;
What you're thinking of as an extension is actually a creation.
int i = 1;
double d = i;
is the same as
Integer i = 1;
Double d = new Double(i);
Use the base class and you can extend it.
public class TestClass1 {
public Number returnMe() {
int i = 1;
return i;
}
}
public class TestClass2 extends TestClass1 {
public Number returnMe() {
double d = 1;
return d;
}
}
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
overriding with different return value type Xue Feng <xf.lotus@yahoo.com> - 2015-07-16 21:04 +0800
Re: overriding with different return value type Jeff Higgins <jeff@invalid.invalid> - 2015-07-16 10:04 -0400
Re: overriding with different return value type e.d.programmer@gmail.com - 2015-07-16 07:23 -0700
Re: overriding with different return value type Jeff Higgins <jeff@invalid.invalid> - 2015-07-16 11:20 -0400
Re: overriding with different return value type Lew <lewbloch@gmail.com> - 2015-07-16 17:13 -0700
csiph-web