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.

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:

public whatever whateverMethod(Object... arguments) {
  // whatever
}

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.

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

hello,
I am beginner in Java and need somebody to help me can you help me to solve my assignment.

Comments
Stop spamming this request into multiple threads.

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.

Actually i m beginner. I tried whatever u said but i have problem.
suppose there is code like,

methodname (Object argument1)   //argument1 is instance of class 'abc'
{
       argument1.xyz();  //xyz is method in class abc,
}

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

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:

methodname (Object argument1)   //argument1 is instance of class 'abc'
{
       (abc)argument1.xyz();  //xyz is method in class abc,
}

It will again throw an Exception if you use as input an object that is not abc

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).

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

public static void methodname(Object argument1)
    {
        if (argument1 instanceof abc) 
        {
            ((abc) argument1).xyz();
        }
    }

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

i can't typecast it because any class like 'abc' can call 'methodname'. We don't know class name in advance. similarly can't use interface or inheritance because we don't know the class name in advance. Is there any other way to do this? Thank you

Reflection (to see if the method exists). Search for it. But I haven't got a clue what kind of a screwed up design you have.

Also, with an interface, you do not need to know the name of the class of the object being passed in, but whoever wrote that class needs to have either implemented the interface, or extended the base class. Your method need know nothing more about it.

I makes absolutely no sense to just assume that an Object will have a specific method when there is no prior defintion of the method. And that is exactly what the compiler is telling you with that error message.

This question has already been answered. Start a new discussion instead.