Results 1 to 5 of 5
  1. #1
    hedefalk is offline Member
    Join Date
    Jul 2007
    Posts
    3
    Rep Power
    0

    Default 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:

    Java 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

  2. #2
    mary is offline Member
    Join Date
    Jul 2007
    Posts
    51
    Rep Power
    0

    Default

    both method from B class do the same thing, they have the same parameters, check it:
    you have this
    Java 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:
    Java Code:
    class A {
    public [COLOR="Red"]Integer id([/COLOR]Request<Integer> request) {
    return new Integer(0);
    }
    
    public Double id(Request<Double> request) {
    return new Double(0.0);
    }
    good luck

  3. #3
    hedefalk is offline Member
    Join Date
    Jul 2007
    Posts
    3
    Rep Power
    0

    Default

    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.

  4. #4
    hedefalk is offline Member
    Join Date
    Jul 2007
    Posts
    3
    Rep Power
    0

    Default

    I'll try to make an even simpler example to focus on the point of my question:

    Java 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

  5. #5
    mary is offline Member
    Join Date
    Jul 2007
    Posts
    51
    Rep Power
    0

    Default

    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"

Similar Threads

  1. Replies: 3
    Last Post: 06-05-2010, 09:05 PM
  2. Replies: 2
    Last Post: 03-26-2010, 06:12 PM
  3. Help with this code, overload the constructor
    By zoe in forum New To Java
    Replies: 12
    Last Post: 11-03-2008, 03:55 AM
  4. Override Methods In NetBeans IDE
    By JavaForums in forum NetBeans
    Replies: 0
    Last Post: 07-31-2007, 12:13 AM
  5. The return type
    By Marcus in forum New To Java
    Replies: 1
    Last Post: 07-05-2007, 07:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •