#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2005
    Location
    Louisiana
    Posts
    15
    Rep Power
    0

    Simple question about function overloading...


    What exactly is function overloading?
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,040
    Rep Power
    1291
    Originally Posted by ineedajob
    What exactly is function overloading?
    In Java, they're called methods.

    Method overloading is having two or more methods in the same class definition with the same name but different parameter lists.

    Comments on this post

    • crownjewel82 agrees
  4. #3
  5. PHP & Java Error Master
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    My Computer
    Posts
    1,219
    Rep Power
    0
    Example:
    java Code:
    import java.util.Properties;
     
    public class TestOverload
    {
    	public void sendMail(String to, String subject, String messageBody)
    	{
    		// do your mail stuff
    	}
     
    	public void sendMail(Properties mailProps)
    	{
    		// here you'd loop through the map grabbing what you need to send the mail
    		// instead of using the string vars in the previous method
    	}
    }

    So in this example, if you called this method and passed it 3 String parameters, it'd use the first method, but if you sent it a Properties map it'd use the second one. This is quite useful when you have methods that do the exact same thing, but want to send different parameters to it (saving you having to have the method take in a ton of parameters to account for every situation).

    Comments on this post

    • Yawmark agrees
    • crownjewel82 agrees
    Last edited by StevenC; February 1st, 2006 at 01:39 PM.
  6. #4
  7. No Profile Picture
    Cypher
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2004
    Location
    La Jolla, CA
    Posts
    121
    Rep Power
    16
    It might be good to mention that you cannot overload a method by simply changing it's return type. There must be a distinction in the parameter list so java knows which method to use at runtime

IMN logo majestic logo threadwatch logo seochat tools logo