Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-31-2007, 12:11 PM
Member
 
Join Date: Jul 2007
Posts: 4
Syed.muddasir is on a distinguished road
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");
}
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-31-2007, 01:03 PM
Member
 
Join Date: Jul 2007
Posts: 72
Swamipsn is on a distinguished road
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");

}
}
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-31-2007, 01:21 PM
Member
 
Join Date: Jul 2007
Posts: 4
Syed.muddasir is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-31-2007, 03:22 PM
Senior Member
 
Join Date: Jul 2007
Posts: 134
brianhks will become famous soon enough
Use the instanceof operator.
Code:
if (object instanceof Double) printObject((Double)object); else printObject((String)object);
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 07-31-2007, 03:25 PM
Member
 
Join Date: Jul 2007
Posts: 4
Syed.muddasir is on a distinguished road
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.....
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 07-31-2007, 03:29 PM
Senior Member
 
Join Date: Jul 2007
Posts: 134
brianhks will become famous soon enough
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.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 07-31-2007, 03:34 PM
Member
 
Join Date: Jul 2007
Posts: 4
Syed.muddasir is on a distinguished road
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.....
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
getting dynamically generated valus abhiN JavaServer Pages (JSP) and JSTL 2 03-29-2008 11:58 AM
How to create widgets dynamically sarbuland Advanced Java 0 02-06-2008 09:08 PM
Dynamically changing the display abhiN JavaServer Pages (JSP) and JSTL 1 01-23-2008 12:19 AM
Overloading? padutch2 New To Java 2 12-31-2007 04:26 AM
Method/Operator Overloading Java Tip Java Tips 0 12-01-2007 09:33 PM


All times are GMT +3. The time now is 09:54 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org