473,387 Members | 1,592 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Determining and invoking methods of a class

Hi,,
I am new to programming languages..

I want this problem to be resolved.

Consider a situation of three class.. C1, C2 & C3.

Class C3 has a method that receives the object as the arguement.

class C3 {
public void xxxxx(Object ob)
}

I want the arguement of the method to be capable of taking the object of any class and display/call the methods of the class object that is passed to the method.

For eg: if C1 object is passed first time.. thn the methods of C1 should be able to access..

if C2 object is passed next time. thn the methods of C2 should be able to access..

I thank you in advance for solving my problem
Sep 10 '07 #1
16 1903
madhoriya22
252 100+
Hi,,
I am new to programming languages..

I want this problem to be resolved.

Consider a situation of three class.. C1, C2 & C3.

Class C3 has a method that receives the object as the arguement.

class C3 {
public void xxxxx(Object ob)
}

I want the arguement of the method to be capable of taking the object of any class and display/call the methods of the class object that is passed to the method.

For eg: if C1 object is passed first time.. thn the methods of C1 should be able to access..

if C2 object is passed next time. thn the methods of C2 should be able to access..

I thank you in advance for solving my problem
Hi,
Collect ur object in object of class Object. Inside method check
Expand|Select|Wrap|Line Numbers
  1.  
  2. if(ob instanceOf C1) {
  3.  
  4. //call C1 class method
  5. }else if(ob instanceOf C2) {
  6.  
  7. //call C2 class method
  8. }else if(ob instanceOf C3) {
  9.  
  10. //call C3 class method
  11. }
  12.  
This way you can do it. :)
Sep 10 '07 #2
Nepomuk
3,112 Expert 2GB
Hi,,
I am new to programming languages..

I want this problem to be resolved.

Consider a situation of three class.. C1, C2 & C3.

Class C3 has a method that receives the object as the arguement.

class C3 {
public void xxxxx(Object ob)
}

I want the arguement of the method to be capable of taking the object of any class and display/call the methods of the class object that is passed to the method.

For eg: if C1 object is passed first time.. thn the methods of C1 should be able to access..

if C2 object is passed next time. thn the methods of C2 should be able to access..

I thank you in advance for solving my problem
Hi s16kumar! Welcome to TSDN!
Do I understand you correctly, you want to have three classes like this:
Expand|Select|Wrap|Line Numbers
  1. public class C1 {
  2.    public void doSomething()
  3.    {
  4.       System.out.println("Hi, this is C1.doSomething()!");
  5.    }
  6. }
  7.  
Expand|Select|Wrap|Line Numbers
  1. public class C2 {
  2.    public void doSomethingElse()
  3.    {
  4.       System.out.println("Hi, this is C2.doSomethingElse()!");
  5.    }
  6. }
  7.  
Expand|Select|Wrap|Line Numbers
  1. public class C3 {
  2.    private Object myObject;
  3.    public void setObject(Object obj)
  4.    {
  5.       myObject = obj;
  6.    }
  7.    public void useObject(String method)
  8.    {
  9.       // use the method "method" from myObject
  10.    }
  11. }
  12.  
Is that correct? If so, maybe getClass and getDeclaredMethod might help. (They look like it, but I haven't tested them yet.)

Greetings,
Nepomuk
Sep 10 '07 #3
Nepomuk
3,112 Expert 2GB
Hi,
Collect ur object in object of class Object. Inside method check
Expand|Select|Wrap|Line Numbers
  1.  
  2. if(ob instanceOf C1) {
  3.  
  4. //call C1 class method
  5. }else if(ob instanceOf C2) {
  6.  
  7. //call C2 class method
  8. }else if(ob instanceOf C3) {
  9.  
  10. //call C3 class method
  11. }
  12.  
This way you can do it. :)
That will work, as long as you only have those two methods (C1 and C2) that you'll want to call with your code. My solution should work for any Object (I hope).

Greetings,
Nepomuk
Sep 10 '07 #4
Hi,
Collect ur object in object of class Object. Inside method check
Expand|Select|Wrap|Line Numbers
  1.  
  2. if(ob instanceOf C1) {
  3.  
  4. //call C1 class method
  5. }else if(ob instanceOf C2) {
  6.  
  7. //call C2 class method
  8. }else if(ob instanceOf C3) {
  9.  
  10. //call C3 class method
  11. }
  12.  
This way you can do it. :)
Thanks for your reply.

But the problem is i dont know as to how many and wht are the class that i need to check with the object.(Like in this case.. its C1 and C2 are known)..

How do i generally have an object.. which can invoke the methods of the class that is passed to it.. at runtime..

Eagerly waiting for ur valuable reply..
Sep 10 '07 #5
Nepomuk
3,112 Expert 2GB
Thanks for your reply.

But the problem is i dont know as to how many and wht are the class that i need to check with the object.(Like in this case.. its C1 and C2 are known)..

How do i generally have an object.. which can invoke the methods of the class that is passed to it.. at runtime..

Eagerly waiting for ur valuable reply..
Check my answer (#3) for that - it should work. If you have problems with it, ask specific questions.

Greetings,
Nepomuk
Sep 10 '07 #6
r035198x
13,262 8TB
If you know the names of the classes and the methods you want to call on them then just test the type using instanceOf and cast the Objects to the relevant class before calling the methods. Otherwise you may need to play around with reflection.
Sep 10 '07 #7
madhoriya22
252 100+
Check my answer (#3) for that - it should work. If you have problems with it, ask specific questions.

Greetings,
Nepomuk
Hi Nepomuk,
Nice description buddy .. You are correct! .. My solution was not generalized :)
Sep 10 '07 #8
Nepomuk
3,112 Expert 2GB
Hi Nepomuk,
Nice description buddy .. You are correct! .. My solution was not generalized :)
Hi Madhoriya! I do my best! ^^
Sep 10 '07 #9
Hi guys..

The reply was right.. But after i accept the object of the class in the object of type Object. The problem is after that i am not able to call the methods of the class object passed to it. The method getClass() gives me the name of the orignal class.. and the method getDeclaredMethod also does not help..

Thank you,..
Sep 10 '07 #10
madhoriya22
252 100+
Hi guys..

The reply was right.. But after i accept the object of the class in the object of type Object. The problem is after that i am not able to call the methods of the class object passed to it. The method getClass() gives me the name of the orignal class.. and the method getDeclaredMethod also does not help..

Thank you,..
Hi,
Can show through some relevant code that how you are doing this ?
Sep 10 '07 #11
r035198x
13,262 8TB
Use reflection
Sep 10 '07 #12
Lets consider the following example..

1)
Classs Customer{
string custName;
public Customer(String name) {
this.custName = name;
}
public String getCustName(){
return this.cusName;
}
}

2)Class Abc{
Customer c1 = new Customer("John");
MainFile mf = new MainFile();
mf.sample(c1);

3)

Class MainFile{
public void sample(Object ob){
// Now i want to call the methods of the class Customer..
// Here i dont know if the object ob has the object of the class Customer or some other classs.. what ever class's object ob has.. it should call those methods.. The mainFile class does not know which class object is passed..

}

Plz Let me know if this info is fine...
Sep 10 '07 #13
r035198x
13,262 8TB
Lets consider the following example..

1)
Classs Customer{
string custName;
public Customer(String name) {
this.custName = name;
}
public String getCustName(){
return this.cusName;
}
}

2)Class Abc{
Customer c1 = new Customer("John");
MainFile mf = new MainFile();
mf.sample(c1);

3)

Class MainFile{
public void sample(Object ob){
// Now i want to call the methods of the class Customer..
// Here i dont know if the object ob has the object of the class Customer or some other classs.. what ever class's object ob has.. it should call those methods.. The mainFile class does not know which class object is passed..

}

Plz Let me know if this info is fine...
1.) Use code tags everytime when posting code.
2.) I was going to ask you to use reflection but I've already done that.
Sep 10 '07 #14
Nepomuk
3,112 Expert 2GB
I've been experimenting and here's a working example:
Expand|Select|Wrap|Line Numbers
  1. import java.lang.reflect.Method;
  2.  
  3. public class C3 {
  4.  
  5.     public static void main(String[] args) {
  6.         C1 c1 = new C1();
  7.         C2 c2 = new C2();
  8.         useClass(c1, "doSomething");
  9.         useClass(c2, "doSomethingElse");
  10.         useClass(c2, "doSomethingThird");
  11.     }
  12.     private static void useClass(Object obj, String method)
  13.     {
  14.         try
  15.         {
  16.             Class params[] = {};
  17.             Method meth = obj.getClass().getDeclaredMethod(method, params);
  18.             Object paramsObj[] = {};
  19.             meth.invoke(obj, paramsObj);
  20.         }
  21.         catch(NoSuchMethodException nsme)
  22.         {
  23.             System.out.println("No method " + method + " in " + obj.getClass());
  24.         }
  25.         catch(Exception e)
  26.         {
  27.             e.printStackTrace();
  28.         }
  29.     }
  30. }
  31. class C1 {
  32.     void doSomething()
  33.     {
  34.         System.out.println("Using C1.doSomething()");
  35.     }
  36. }
  37. class C2 {
  38.     void doSomethingElse()
  39.     {
  40.         System.out.println("Using C2.doSomethingElse()");
  41.     }
  42.     void doSomethingThird()
  43.     {
  44.         System.out.println("Using C2.doSomethingThird()");
  45.     }
  46. }
  47.  
If you need help understanding it, do ask!
Just hope, it's not considered to much spoon feeding. ^^ If so, a Admin may feel free to delete it.

Greetings,
Nepomuk
Sep 10 '07 #15
r035198x
13,262 8TB
I've been experimenting and here's a working example:
Expand|Select|Wrap|Line Numbers
  1. import java.lang.reflect.Method;
  2.  
  3. public class C3 {
  4.  
  5.     public static void main(String[] args) {
  6.         C1 c1 = new C1();
  7.         C2 c2 = new C2();
  8.         useClass(c1, "doSomething");
  9.         useClass(c2, "doSomethingElse");
  10.         useClass(c2, "doSomethingThird");
  11.     }
  12.     private static void useClass(Object obj, String method)
  13.     {
  14.         try
  15.         {
  16.             Class params[] = {};
  17.             Method meth = obj.getClass().getDeclaredMethod(method, params);
  18.             Object paramsObj[] = {};
  19.             meth.invoke(obj, paramsObj);
  20.         }
  21.         catch(NoSuchMethodException nsme)
  22.         {
  23.             System.out.println("No method " + method + " in " + obj.getClass());
  24.         }
  25.         catch(Exception e)
  26.         {
  27.             e.printStackTrace();
  28.         }
  29.     }
  30. }
  31. class C1 {
  32.     void doSomething()
  33.     {
  34.         System.out.println("Using C1.doSomething()");
  35.     }
  36. }
  37. class C2 {
  38.     void doSomethingElse()
  39.     {
  40.         System.out.println("Using C2.doSomethingElse()");
  41.     }
  42.     void doSomethingThird()
  43.     {
  44.         System.out.println("Using C2.doSomethingThird()");
  45.     }
  46. }
  47.  
If you need help understanding it, do ask!
Just hope, it's not considered to much spoon feeding. ^^ If so, a Admin may feel free to delete it.

Greetings,
Nepomuk
What would really help the OP is to read a reflection tutorial. But they are ignoring me anyway ...
Sep 10 '07 #16
Nepomuk
3,112 Expert 2GB
What would really help the OP is to read a reflection tutorial. But they are ignoring me anyway ...
This one? Cause even if the OP doesn't read it, I'm interested...

Greetings,
Nepomuk
Sep 10 '07 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: seller | last post by:
coded this method in bean. The intent was for the bean to run all the setters using the values in a vector argument. here's the code: public void loadSelf(Vector v) throws Exception { String...
6
by: Patrick | last post by:
Following earlier discussions about invoking a .NET class library via ..NET-COM Interop (using regasm /tlb) at...
4
by: Alfonso Morra | last post by:
Hi, I have a variable that stores a pointer to a base class. I am using this variable to store pointers to objects of the base class, as well as pointers to other derived classes. However,...
3
by: Ben Fidge | last post by:
Hi Is it possible, using Reflection, to determine at runtime the method(s) that are provided as handler for a given controls events? Also, once discovered, is it possible to manually invoke...
25
by: MuZZy | last post by:
Hi, I'm currently rewriting some functionality which was using multithredaing for retrieving datasets from database and updating a grid control. I found that the grids (Infragistics UltraGrid,...
12
by: Hemanth | last post by:
Hi, I have a base class with a static constructor and some abstract methods. Derived classes implement these methods. From articles on the web, it appears that there is no guarentee that this...
2
by: shanmani | last post by:
Hi, I am developing a .NET application which will invoke the methods from different COM / .NET DLLs. While invoking methods from .NET DLLs, I am encountering the following error. I have also...
1
by: Suds | last post by:
Hi, I'm having an issue with invoking a Generic method that takes Generic Arguments. My method signature is public void GenericMethodWithGenericArguments<E, V>(List<EtheFirstList,...
3
by: Gordon | last post by:
I am currently working on some code for my CMS that creates a site folder, then creates all the necessary child folders inside it. The method that creates folders needs to insert into 2 tables so...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.