how to send object of any class dynamically as a parameter to a method

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Mar 2008
Posts: 3
Reputation: pavya133 is an unknown quantity at this point 
Solved Threads: 0
pavya133 pavya133 is offline Offline
Newbie Poster

how to send object of any class dynamically as a parameter to a method

 
0
  #1
Mar 6th, 2008
there is problem in java program. I want to send object of a particular class as a parameter to a method in other class dynamically. e.g Suppose there is method named 'insert' in a class 'Daobase' and parameter to this method is object of any class that is i want to send it dynamically. or is there any other way to do this? please help me out, thank you.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,467
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 267
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: how to send object of any class dynamically as a parameter to a method

 
0
  #2
Mar 6th, 2008
What are you talking about? Send a parameter dynamically? No idea what you mean there.

However, why don't you just make the defintion as follows:

  1. public whatever whateverMethod(Object... arguments) {
  2. // whatever
  3. }

Now you can pass it as much of anything as you like.

The necessity for something like this, however, means you have a bad design and need to rethink your application design.
Last edited by masijade; Mar 6th, 2008 at 7:21 am. Reason: typo
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,718
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 230
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: how to send object of any class dynamically as a parameter to a method

 
0
  #3
Mar 6th, 2008
I think, masijade, that he wants to call this method:whateverMethod(Object obj) with any kind of object and for the method to do what is necessary every time dynamically. Meaning that I don't think that he intends to check what type of class he is passing in order to do casting.
Perhaps if he wants to save the object he is passing, he should consider serializing/desirializing
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 8
Reputation: bmbvm5 is an unknown quantity at this point 
Solved Threads: 1
bmbvm5 bmbvm5 is offline Offline
Newbie Poster

Re: how to send object of any class dynamically as a parameter to a method

 
-1
  #4
Mar 6th, 2008
hello,
I am beginner in Java and need somebody to help me can you help me to solve my assignment.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,467
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 267
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: how to send object of any class dynamically as a parameter to a method

 
0
  #5
Mar 6th, 2008
Originally Posted by javaAddict View Post
I think, masijade, that he wants to call this method:whateverMethod(Object obj) with any kind of object and for the method to do what is necessary every time dynamically. Meaning that I don't think that he intends to check what type of class he is passing in order to do casting.
Perhaps if he wants to save the object he is passing, he should consider serializing/desirializing
My "solution" is the same thing, simply "widened" with varargs so that the number is not limited to only one. That is, of course, even worse, but the principle in both remain the same, as well as my last statement. The need to do this is usually (not always, but usually) a result of bad design. The very least he could do is to create a base abstract class to inherit from, or an interface to implement, and use that abstract class or interface as the parameter. That, at least, dictates some restriction.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 3
Reputation: pavya133 is an unknown quantity at this point 
Solved Threads: 0
pavya133 pavya133 is offline Offline
Newbie Poster

Re: how to send object of any class dynamically as a parameter to a method

 
0
  #6
Mar 7th, 2008
Actually i m beginner. I tried whatever u said but i have problem.
suppose there is code like,
  1. methodname (Object argument1) //argument1 is instance of class 'abc'
  2. {
  3. argument1.xyz(); //xyz is method in class abc,
  4. }
but there is error 'method name xyz is unable to find.' I am saying it as dynamically because user can call 'methodname' using object of any class like 'abc'. so i can't typecast it. Thank you
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,718
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 230
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: how to send object of any class dynamically as a parameter to a method

 
0
  #7
Mar 7th, 2008
There is an error because argument1 is an Object and can't call the xyz method. Remember
the class abc may be an object(meaning you can use it as an argument at the method)
but an object is NOT an abc class(meaning that the argument Object does not have a method named xyz).
That's because you could have called that method with any other argument (an other object but not an abc class) and then your method would throw an Exception.
If you want it to work try:
  1. methodname (Object argument1) //argument1 is instance of class 'abc'
  2. {
  3. (abc)argument1.xyz(); //xyz is method in class abc,
  4. }
It will again throw an Exception if you use as input an object that is not abc
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,467
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 267
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: how to send object of any class dynamically as a parameter to a method

 
0
  #8
Mar 7th, 2008
Because, the only methods that all objects have are those methods defined in Object.

What you want to do, doesn't exist. If want something at least a little bit like this, then read reply #5 again (the part about abstract classes and interfaces).
Last edited by masijade; Mar 7th, 2008 at 3:22 pm. Reason: And man, am I slow.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 10
Reputation: Zork'nPalls is an unknown quantity at this point 
Solved Threads: 2
Zork'nPalls Zork'nPalls is offline Offline
Newbie Poster

Re: how to send object of any class dynamically as a parameter to a method

 
0
  #9
Mar 7th, 2008
javaAddicts suggestion is great, but it won't work.
you need to put parenthesis around the "(abc) argument1" so that the Object is casted and not the void. Also, if you don't want an error thrown when argument1 is not an abc, use the instanceof keyword, as shown
  1. public static void methodname(Object argument1)
  2. {
  3. if (argument1 instanceof abc)
  4. {
  5. ((abc) argument1).xyz();
  6. }
  7. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 8
Reputation: madhusamala is an unknown quantity at this point 
Solved Threads: 1
madhusamala madhusamala is offline Offline
Newbie Poster

Re: how to send object of any class dynamically as a parameter to a method

 
0
  #10
Mar 8th, 2008
dude, i got your doubt. You want to send one object reference as a parameter to a method in another class. But both the classes must have an inheritance relationship in order to perform dynamic polymorphism

ex.
class A
{
void m1()
{
--
}
}
class B
{
A a;
void m2(A a)
{
a=new B();
---
}
}
Here you are performing dynamic polymorphism
But by defalut you can't do that unless you extend B from A.
Or you can pass an Object reference direcltly because
all the classes are inherited from Object class
so you must write B class as the following
class B extends A
{
A a;
void m2(A a)
{
a=new B();
---
}
}
if you have any doubts again give me the indetail code
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum


Views: 1726 | Replies: 11
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC