Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > implementing method with different return type

Reply
Thread Tools

implementing method with different return type

 
 
Pradeep Kumar
Guest
Posts: n/a
 
      05-05-2004
Hi all ......

suppose a code ..

interface one {
Object methodOne();
}

class two implements one {
public String methodOne() {
return "";
}
}


now as i implmented a interface "one" in class "two".. i changed the return
type to a subclass of Object "String".. this is perfectlly valid in term of
logic or concept... but java compiler is giving an error ... compiler says
that prototype is not same .. this is fine ...

now my question is why java compiler make such type of restriction ... i
think there is no problem in declaring return type as any subclass ...

any suggestion / answer / hint ?????????????

--
Pradeep Kumar
----------------------------------------------------------
What's right isn't always popular, and what's popular isn't always right.
----------------------------------------------------------



 
Reply With Quote
 
 
 
 
VisionSet
Guest
Posts: n/a
 
      05-05-2004

"Pradeep Kumar" <(E-Mail Removed)> wrote in message
news:c7alju$1lkrp$(E-Mail Removed)-berlin.de...
> Hi all ......
>
> suppose a code ..
>
> interface one {
> Object methodOne();
> }
>
> class two implements one {
> public String methodOne() {
> return "";
> }
> }
>
>


The signature must be the same.
Try:

> interface one {
> Object methodOne();
> }
>
> class two implements one {
> public Object methodOne() {
> return "";
> }
> }


And cast the result back to String after calling methodOne()

--
Mike W


 
Reply With Quote
 
 
 
 
Peter Kirk
Guest
Posts: n/a
 
      05-05-2004
"VisionSet" <(E-Mail Removed)> skrev i en meddelelse
news:co5mc.4396$7S2.2246@newsfe1-win...
>
> "Pradeep Kumar" <(E-Mail Removed)> wrote in message
> news:c7alju$1lkrp$(E-Mail Removed)-berlin.de...
> > interface one {
> > Object methodOne();
> > }
> >
> > class two implements one {
> > public String methodOne() {
> > return "";
> > }
> > }

>
> The signature must be the same.


I think the "signature" is the same (isn't it the case that "signature" is
defined as the method's name and parameters, but does not include its return
type?).

It is the return type that the OP wants to be different. And this is not
possible in Java, except as you specify below...

> Try:
>
> > interface one {
> > Object methodOne();
> > }
> >
> > class two implements one {
> > public Object methodOne() {
> > return "";
> > }
> > }

>
> And cast the result back to String after calling methodOne()


 
Reply With Quote
 
Dale King
Guest
Posts: n/a
 
      05-05-2004
"Pradeep Kumar" <(E-Mail Removed)> wrote in message
news:c7alju$1lkrp$(E-Mail Removed)-berlin.de...
> Hi all ......
>
> suppose a code ..
>
> interface one {
> Object methodOne();
> }
>
> class two implements one {
> public String methodOne() {
> return "";
> }
> }
>
>
> now as i implmented a interface "one" in class "two".. i changed the

return
> type to a subclass of Object "String".. this is perfectlly valid in term

of
> logic or concept... but java compiler is giving an error ... compiler says
> that prototype is not same .. this is fine ...
>
> now my question is why java compiler make such type of restriction ... i
> think there is no problem in declaring return type as any subclass ...
>
> any suggestion / answer / hint ?????????????


This restriction will be removed in JDK 1.5.

--
Dale King
Blog: http://daleking.homedns.org/Blog


 
Reply With Quote
 
VisionSet
Guest
Posts: n/a
 
      05-05-2004
"Peter Kirk" <peter> wrote in message news:4098f087$(E-Mail Removed)...
> "VisionSet" <(E-Mail Removed)> skrev i en meddelelse
> news:co5mc.4396$7S2.2246@newsfe1-win...
> >
> > "Pradeep Kumar" <(E-Mail Removed)> wrote in message
> > news:c7alju$1lkrp$(E-Mail Removed)-berlin.de...
> > > interface one {
> > > Object methodOne();
> > > }
> > >
> > > class two implements one {
> > > public String methodOne() {
> > > return "";
> > > }
> > > }

> >
> > The signature must be the same.

>
> I think the "signature" is the same (isn't it the case that "signature" is
> defined as the method's name and parameters, but does not include its

return
> type?).
>
> It is the return type that the OP wants to be different. And this is not
> possible in Java, except as you specify below...


You are correct, I was being sloppy for clarity, if that makes any sense
Perhaps someone knows the term for the full first line of code that defines
a method?

--
Mike W


 
Reply With Quote
 
Peter
Guest
Posts: n/a
 
      05-05-2004

"Dale King" <kingd[at]tmicha[dot]net> skrev i en meddelelse
news:(E-Mail Removed)...
> "Pradeep Kumar" <(E-Mail Removed)> wrote in message
> news:c7alju$1lkrp$(E-Mail Removed)-berlin.de...

<snip>
> > now my question is why java compiler make such type of restriction ... i
> > think there is no problem in declaring return type as any subclass ...
> >
> > any suggestion / answer / hint ?????????????

>
> This restriction will be removed in JDK 1.5.


Does this also mean that methods could be "overloaded" like this:

void foo();
int foo();
Long foo();

?


 
Reply With Quote
 
Christophe Vanfleteren
Guest
Posts: n/a
 
      05-05-2004
Peter wrote:

>> > now my question is why java compiler make such type of restriction ...
>> > i think there is no problem in declaring return type as any subclass
>> > ...
>> >
>> > any suggestion / answer / hint ?????????????

>>
>> This restriction will be removed in JDK 1.5.

>
> Does this also mean that methods could be "overloaded" like this:
>
> void foo();
> int foo();
> Long foo();
>
> ?


No, you can substitute the return type for one of it's subclasses (eg, from
Object to String). That way, you can still treat the subtype as its
supertype, which wouldn't be possible in your examples.

--
Kind regards,
Christophe Vanfleteren
 
Reply With Quote
 
Joona I Palaste
Guest
Posts: n/a
 
      05-05-2004
Christophe Vanfleteren <(E-Mail Removed)> scribbled the following:
> Peter wrote:
>>> > now my question is why java compiler make such type of restriction ...
>>> > i think there is no problem in declaring return type as any subclass
>>> > ...
>>> >
>>> > any suggestion / answer / hint ?????????????
>>>
>>> This restriction will be removed in JDK 1.5.

>>
>> Does this also mean that methods could be "overloaded" like this:
>>
>> void foo();
>> int foo();
>> Long foo();
>>
>> ?


> No, you can substitute the return type for one of it's subclasses (eg, from
> Object to String). That way, you can still treat the subtype as its
> supertype, which wouldn't be possible in your examples.


IMHO overridden methods should be able to widen the types of their
parameters, and to narrow the types of their return values. This would
make the contract with the overridden method more generous than that
with the original method, so the original contract would still work.
The opposite is forbidden: overridden methods should not be able to
narrow the types of their parameters, nor widen the types of their
return values. The maxim for this is: the caller of the method should
not need to know, or care about, whether the method has been
overridden.
I also feel that methods should not be overloaded based on return types.
Because this would mean that the right methods is only chosen by how the
resulting value is used - assigned to a variable of a specific type, for
example, and so things would get extremely hairy.

Imagine a case like this, for example:

public class A {
public Object foo() { /* ... */ }
public String foo() { /* ... */ }
}

/* ... */
System.out.println("Hello, " + new A().foo());

or even a case like this:

public class A {
public Object foo() { /* ... */ }
public String foo() { /* ... */ }
}

/* ... */
new A().foo();

How should the Java compiler be able to decide the correct
implementation? IMHO, making a decision such as this based on code
*outside* the method call itself causes hopelessly complicated syntax
rules.

--
/-- Joona Palaste ((E-Mail Removed)) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Stronger, no. More seductive, cunning, crunchier the Dark Side is."
- Mika P. Nieminen
 
Reply With Quote
 
Joona I Palaste
Guest
Posts: n/a
 
      05-05-2004
Dale King <(E-Mail Removed)> scribbled the following:
> Hello, Peter!
> You wrote:
>> "Dale King" <kingd[at]tmicha[dot]net> skrev i en meddelelse
>> news:(E-Mail Removed)...
>> > "Pradeep Kumar" <(E-Mail Removed)> wrote in message
>> > news:c7alju$1lkrp$(E-Mail Removed)-berlin.de...

>> <snip>
>> > > now my question is why java compiler make such type of

> restriction ... i
>> > > think there is no problem in declaring return type as any

> subclass ...
>> > >
>> > > any suggestion / answer / hint ?????????????
>> >
>> > This restriction will be removed in JDK 1.5.

>>
>> Does this also mean that methods could be "overloaded" like

> this:
>>
>> void foo();
>> int foo();
>> Long foo();


> No, you can only override with a subclass of the return type of
> the inherited method signature. Previously, it had to be the
> exact same class and in 1.5 they relax that to include
> subclasses. It's caled covariant return types and is a necessity
> with the introduction of generics.


Over*ride*, that is, not over*load*. The method in the subclass does the
job of the same method in the superclass. The dispatch is resolved at
run-time, unlike in overloading, where it is resolved completely
statically at compile-time.
You can *never* overload methods on return types. With Java 1.5 you can
override them on return types under some circumstances.

--
/-- Joona Palaste ((E-Mail Removed)) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Holy Banana of this, Sacred Coconut of that, Magic Axolotl of the other."
- Guardian in "Jinxter"
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      05-05-2004
On Wed, 5 May 2004 17:40:35 +0530, "Pradeep Kumar"
<(E-Mail Removed)> wrote or quoted :

> compiler says
>that prototype is not same .. this is fine ...


No it is not fine. You cannot have two methods that differ only in
return type. The problem is, how would Java decide which of them to
use?

consider:

(new Dog).bark();

Where you have two kinds of bark methods, on that returns a Tree and
one that returns a Noise. How could Java decide which to invoke? It
needs the arguments to differ to decide.



--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
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
How to override a method with different return type sandy Java 3 11-15-2005 06:35 PM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM
overriding method with different return type Paul J. Lucas Java 5 04-12-2004 09:45 AM
Re: How do I access another type's method from one type's method Howard C++ 2 07-04-2003 12:08 PM
Re: How do I access another type's method from one type's method Rolf Magnus C++ 1 07-04-2003 02:38 AM



Advertisments