Results 1 to 7 of 7
Thread: Overloading methods dynamically
- 07-31-2007, 11:11 AM #1
Member
- Join Date
- Jul 2007
- Posts
- 4
- Rep Power
- 0
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");
}
- 07-31-2007, 12:03 PM #2
Member
- Join Date
- Jul 2007
- Posts
- 74
- Rep Power
- 0
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");
}
}
- 07-31-2007, 12:21 PM #3
Member
- Join Date
- Jul 2007
- Posts
- 4
- Rep Power
- 0
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.
- 07-31-2007, 02:22 PM #4
Senior Member
- Join Date
- Jul 2007
- Posts
- 135
- Rep Power
- 0
Use the instanceof operator.
Java Code:if (object instanceof Double) printObject((Double)object); else printObject((String)object);
- 07-31-2007, 02:25 PM #5
Member
- Join Date
- Jul 2007
- Posts
- 4
- Rep Power
- 0
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.....
- 07-31-2007, 02:29 PM #6
Senior Member
- Join Date
- Jul 2007
- Posts
- 135
- Rep Power
- 0
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.
- 07-31-2007, 02:34 PM #7
Member
- Join Date
- Jul 2007
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
getting dynamically generated valus
By abhiN in forum JavaServer Pages (JSP) and JSTLReplies: 2Last Post: 03-29-2008, 10:58 AM -
How to create widgets dynamically
By sarbuland in forum Advanced JavaReplies: 0Last Post: 02-06-2008, 08:08 PM -
Dynamically changing the display
By abhiN in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 01-22-2008, 11:19 PM -
Overloading?
By padutch2 in forum New To JavaReplies: 2Last Post: 12-31-2007, 03:26 AM -
Method/Operator Overloading
By Java Tip in forum Java TipReplies: 0Last Post: 12-01-2007, 08:33 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks