Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-10-2007, 04:05 PM
Member
 
Join Date: Jul 2007
Posts: 3
hedefalk is on a distinguished road
return type determines override/overload?
Hi!

This might really seem like a beginner's question and it probably is, but I want the Expert's answer

Consider the following Java 5-code:

Code:
class Request<T> { private T t; public T getT() { return t; } public void setT(T t) { this.t = t; } } class A { public Integer id(Request<Integer> request) { return new Integer(0); } public Double id(Request<Double> request) { return new Double(0.0); } } class B { public Double id(Request<Integer> request) { return new Double(0.0); } public Double id(Request<Double> request) { return new Double(0.0); } }
In this code class A compiles just fine, but class B doesn't. You get an error saying there are duplicate methods. First off, I was hoping that since the type of a generic parameter is known at compile time, these methods in class B would be overloaded. I know that the generics is only a compile time construction, but I thought that this would work anyway, maybe by inserting this type information in the identifier or something.

This is not my big problem though. What I do not understand is why the same thing works just fine in class A when the only thing I have changed is the return type. In all functional and imperative programming languages I have ever been in contact with the signature of a function or method is always determined by its name and its parameter types. And I would wanna believe that it is the signature that determines if to methods clashes or not and the case of inheritance, if they are overridden or overloaded. But here, a difference in the return type changes this behaviour.

I would be grateful for an explaination!

/Viktor
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-10-2007, 06:27 PM
Member
 
Join Date: Jul 2007
Posts: 51
mary is on a distinguished road
both method from B class do the same thing, they have the same parameters, check it:
you have this
Code:
class A { public Double id(Request<Integer> request) { return new Integer(0); } public Double id(Request<Double> request) { return new Double(0.0); }
and you have to do that:
Code:
class A { public Integer id(Request<Integer> request) { return new Integer(0); } public Double id(Request<Double> request) { return new Double(0.0); }
good luck
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-10-2007, 06:33 PM
Member
 
Join Date: Jul 2007
Posts: 3
hedefalk is on a distinguished road
Yes, I understand that both methods do the same thing. I constructed them as simple as possible to focus on the point that the only difference between the to classes A and B is the return type of the methods.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-11-2007, 02:19 PM
Member
 
Join Date: Jul 2007
Posts: 3
hedefalk is on a distinguished road
I'll try to make an even simpler example to focus on the point of my question:

Code:
class Generic<T> { } class CompilesFine { public int id(Generic<Integer> request) { return 0; } public byte id(Generic<Double> request) { return 0; } } class DoesNotCompile { public int id(Generic<Integer> request) { return 0; } public int id(Generic<Double> request) { return 0; } }
/Viktor
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 07-11-2007, 02:48 PM
Member
 
Join Date: Jul 2007
Posts: 51
mary is on a distinguished road
I tested them, and you're right.
The only thing that I think it's maybe <> make them indistinguishable, and that is the reason about "duplicate method"
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Override Methods In NetBeans IDE JavaForums NetBeans 0 07-31-2007 12:13 AM
Help with this code, overload the constructor zoe New To Java 1 07-25-2007 09:17 PM
Error: invalid method declaration; return type required silvia AWT / Swing 1 07-19-2007 02:51 PM
The return type Marcus New To Java 1 07-05-2007 07:28 AM
method not abstract, does not override actionperformed method. Theman New To Java 1 05-08-2007 07:13 AM


All times are GMT +3. The time now is 06:39 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org