Results 1 to 7 of 7
  1. #1
    Syed.muddasir is offline Member
    Join Date
    Jul 2007
    Posts
    4
    Rep Power
    0

    Default Overloading methods dynamically

    Hi everyone im new to this forum and here is my problem...
    As shown in the code i have two overloaded methods which accepts a Double and string argument but i have an incoming object of unknown instace type
    How can i invoke the method printObject() dynamically.

    Thanks in advance...


    public class Test {
    public static void printObject(Double arg){
    System.out.println("Double" + arg);
    }

    public static void printObject(String arg){
    System.out.println("String" + arg);
    }

    public static void main(String args[])
    {
    Object one = new Double(100);
    Object two = new String ("hello");
    }

  2. #2
    Swamipsn is offline Member
    Join Date
    Jul 2007
    Posts
    74
    Rep Power
    0

    Smile Where

    Too simple!! Pls try forllowing code
    public class Test {
    public static void printObject(double arg){
    System.out.println("Double " + arg);
    }

    public static void printObject(String arg){
    System.out.println("String " + arg);
    }

    public static void main(String args[])
    {
    Test ob1 = new Test();
    ob1.printObject(100.25698);
    ob1.printObject("This is Correct");

    }
    }

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

    Default

    Thks for your response ......
    This would no doubt work but wht im getting is Object reference which is of type object
    eg:Object doub = new Double(546.56);
    I want this object to be resolved during run time.

  4. #4
    brianhks is offline Senior Member
    Join Date
    Jul 2007
    Posts
    135
    Rep Power
    0

    Default

    Use the instanceof operator.
    Java Code:
    if (object instanceof Double)
      printObject((Double)object);
    else
      printObject((String)object);

  5. #5
    Syed.muddasir is offline Member
    Join Date
    Jul 2007
    Posts
    4
    Rep Power
    0

    Default

    I was doing the same thing but i dont wanna use instance coz i have to chk for each type....
    Thts why i thought method overloading could be done.....

  6. #6
    brianhks is offline Senior Member
    Join Date
    Jul 2007
    Posts
    135
    Rep Power
    0

    Default

    It is really not possible when you are handed an Object. The system has nothing to go off of so you have to check what kind of object it really is.

  7. #7
    Syed.muddasir is offline Member
    Join Date
    Jul 2007
    Posts
    4
    Rep Power
    0

    Default

    Thx brian.
    Wht i was thinkin is tht if anything could be achieved using reflection and cloning ,so that i would create an new object of the Objects instance type and call the overloaded method.....

Similar Threads

  1. getting dynamically generated valus
    By abhiN in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 03-29-2008, 10:58 AM
  2. How to create widgets dynamically
    By sarbuland in forum Advanced Java
    Replies: 0
    Last Post: 02-06-2008, 08:08 PM
  3. Dynamically changing the display
    By abhiN in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 01-22-2008, 11:19 PM
  4. Overloading?
    By padutch2 in forum New To Java
    Replies: 2
    Last Post: 12-31-2007, 03:26 AM
  5. Method/Operator Overloading
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-01-2007, 08:33 PM

Posting Permissions

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