View Single Post
  #1 (permalink)  
Old 07-10-2007, 04:05 PM
hedefalk hedefalk is offline
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
Reply With Quote
Sponsored Links