Results 1 to 6 of 6
  1. #1
    garyiskidding is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default void doIt() & String doIt() don't compile togather.

    Hello,

    i want to understand the reason why the following class won't compile and saya that the method has been duplicated.

    Java Code:
    public class A {
    	public void doit() {
    	}
    
    	public String doit() {
    		return "a";
    	}
    }
    I know that it would compile if the two methods had different argumets, but i want to know the reason as to why the java compiler ignores the return type. This is neither a valid overload, nor a valid override, but why does the java language skip method differentiation based on return types, and complain that the two methods above are the same.

    Thanks
    Gary

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    how should java guess which method you mean when you make a call to doit()? so it's logically that the return type can not be considered for overloading!

  3. #3
    garyiskidding is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default

    i was assuming that Java should implement this based on how the method is called. For instance :

    Java Code:
    String s = doIt();
    is assigning a String reference to whatever is being returned from the method, so Java can quickly guess that it needs the 'public String doIt()' method, and not the 'public void doIt()' method. In most cases, this should be straight forward.

    your thoughts?

  4. #4
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by garyiskidding View Post
    i was assuming that Java should implement this based on how the method is called. For instance :

    Java Code:
    String s = doIt();
    is assigning a String reference to whatever is being returned from the method, so Java can quickly guess that it needs the 'public String doIt()' method, and not the 'public void doIt()' method. In most cases, this should be straight forward.

    your thoughts?

    please note that like in c or c++ also in java a method can be called without assigning its return value to a variable. so your example is conflicting with this rule. why do you bother about the overriding rules in java?

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Don't assume. Per definition the return type is not part of the method signature.

  6. #6
    garyiskidding is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

Similar Threads

  1. Replies: 7
    Last Post: 05-14-2010, 04:26 PM
  2. Public static void main (String args[])
    By arefeh in forum New To Java
    Replies: 12
    Last Post: 01-28-2010, 12:58 PM
  3. Replies: 9
    Last Post: 11-23-2008, 12:03 AM
  4. Replies: 5
    Last Post: 01-25-2008, 12:53 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
  •