Results 1 to 5 of 5

Thread: Same signs ??

  1. #1
    toiletpaper is offline Member
    Join Date
    May 2014
    Posts
    43
    Rep Power
    0

    Default Same signs ??

    Hello,


    Method overloading, consists of writing the same method in the same class with different signs.

    My question is, how do i know if they have different signs ? could someone provide me with a few different examples of different signs and same signs (3 or 4) ??

    Thanks in advance,
    Kind Regards,
    toiletpaper

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,870
    Rep Power
    5

    Default Re: Same signs ??

    Almost: they have different signatures. If you google method signatures you'll get far more relevant results.

    A method signature consists of the name and the parameter types. So

    Java Code:
    public void myMethod(int param){
    
    }
    
    public void myMethod(String param){
    
    }
    have different signatures because one accepts an int and the other a String. The same here:

    Java Code:
    public void myMethod(int param1, String param2){
    
    }
    
    public void myMethod(String param1, int param2){
    
    }
    Different signatures because the parameter types do not match up.

    Java does not care about:

    - the return type
    - the name of the parameters

    So this:

    Java Code:
    public String myMethod(String param){
    
    }
    
    public void myMethod(String param){
    
    }
    
    public void myMethod(String mySuperDuperParam){
    
    }
    is all the same signature to Java, and thus you will get duplicate method compile errors.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    toiletpaper is offline Member
    Join Date
    May 2014
    Posts
    43
    Rep Power
    0

    Default Re: Same signs ??

    Quote Originally Posted by gimbal2 View Post
    Almost: they have different signatures. If you google method signatures you'll get far more relevant results.

    A method signature consists of the name and the parameter types. So

    Java Code:
    public void myMethod(int param){
    
    }
    
    public void myMethod(String param){
    
    }
    have different signatures because one accepts an int and the other a String. The same here:

    Java Code:
    public void myMethod(int param1, String param2){
    
    }
    
    public void myMethod(String param1, int param2){
    
    }
    Different signatures because the parameter types do not match up.

    Java does not care about:

    - the return type
    - the name of the parameters

    So this:

    Java Code:
    public String myMethod(String param){
    
    }
    
    public void myMethod(String param){
    
    }
    
    public void myMethod(String mySuperDuperParam){
    
    }
    is all the same signature to Java, and thus you will get duplicate method compile errors.
    Why doesnt the 2nd example have the same sign ? the method takes an int then a string, and in the next one it takes a string then an int, its the same thing, but different parameter names.

  4. #4
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Same signs ??

    The order of parameters is important.
    So there is a difference between String int and int String.

  5. #5
    toiletpaper is offline Member
    Join Date
    May 2014
    Posts
    43
    Rep Power
    0

Similar Threads

  1. polish signs in log-file
    By rafzio4 in forum Java Servlet
    Replies: 3
    Last Post: 10-20-2010, 02:04 PM
  2. [SOLVED] How can I dynanically change math signs
    By CJSLMAN in forum New To Java
    Replies: 10
    Last Post: 10-07-2008, 04:07 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
  •