Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #32042 > unrolled thread

overriding with different return value type

Started byXue Feng <xf.lotus@yahoo.com>
First post2015-07-16 21:04 +0800
Last post2015-07-16 17:13 -0700
Articles 5 — 4 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  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

#32042 — overriding with different return value type

FromXue Feng <xf.lotus@yahoo.com>
Date2015-07-16 21:04 +0800
Subjectoverriding with different return value type
Message-ID<mo8a4l$h99$1@speranza.aioe.org>
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.



[toc] | [next] | [standalone]


#32044

FromJeff Higgins <jeff@invalid.invalid>
Date2015-07-16 10:04 -0400
Message-ID<mo8dqd$7gr$1@dont-email.me>
In reply to#32042
On 07/16/2015 09:04 AM, 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:

I'm not sure what the JDK manual is.

The Java® Language Specification
Java SE 8 Edition has at least this to say
about overriding:

<http://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.4.8>


[toc] | [prev] | [next] | [standalone]


#32045

Frome.d.programmer@gmail.com
Date2015-07-16 07:23 -0700
Message-ID<52bdf1e9-9839-407f-83d0-ba2e031f4826@googlegroups.com>
In reply to#32042
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;
     }
}

[toc] | [prev] | [next] | [standalone]


#32046

FromJeff Higgins <jeff@invalid.invalid>
Date2015-07-16 11:20 -0400
Message-ID<mo8i8h$oi3$1@dont-email.me>
In reply to#32042
On 07/16/2015 09:04 AM, Xue Feng wrote:

 >  I wonder why the clone() method  works in different way.


 From the Java™ Platform, Standard Edition 8
API Specification:

Interface Cloneable
<https://docs.oracle.com/javase/8/docs/api/java/lang/Cloneable.html>

Class Object - clone() method
<https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#clone-->

Other discussion:

Objects and Java
Chapter 8: Cloning, Collections, and Inner Classes
<https://www.artima.com/objectsandjava/webuscript/ClonCollInner1.html>

Josh Bloch on Design
Copy Constructor versus Cloning
<http://www.artima.com/intv/bloch13.html>

See Item 11: Override clone judiciously in:
Bloch, J. (2008). Effective Java. Upper Saddle River, NJ: Addison-Wesley.
<https://en.wikipedia.org/wiki/Special:BookSources/9780321356680>


[toc] | [prev] | [next] | [standalone]


#32050

FromLew <lewbloch@gmail.com>
Date2015-07-16 17:13 -0700
Message-ID<6660a51d-773f-4fea-8e9d-a4a88d02ecff@googlegroups.com>
In reply to#32046
 Xue Feng wrote:
> >  I wonder why the clone() method  works in different way.

Look up "covariant return types". It's in the JLS. 

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web