Determining and invoking methods of a class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • s16kumar
    New Member
    • Sep 2007
    • 4

    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
  • madhoriya22
    Contributor
    • Jul 2007
    • 251

    #2
    Originally posted by s16kumar
    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
    Code:
     
    if(ob instanceOf C1) {
     
    //call C1 class method
    }else if(ob instanceOf C2) {
     
    //call C2 class method
    }else if(ob instanceOf C3) {
     
    //call C3 class method
    }
    This way you can do it. :)
    Last edited by madhoriya22; Sep 10 '07, 08:49 AM. Reason: To add my signature :)

    Comment

    • Nepomuk
      Recognized Expert Specialist
      • Aug 2007
      • 3111

      #3
      Originally posted by s16kumar
      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:
      [CODE=java]
      public class C1 {
      public void doSomething()
      {
      System.out.prin tln("Hi, this is C1.doSomething( )!");
      }
      }
      [/CODE][CODE=java]
      public class C2 {
      public void doSomethingElse ()
      {
      System.out.prin tln("Hi, this is C2.doSomethingE lse()!");
      }
      }
      [/CODE][CODE=java]
      public class C3 {
      private Object myObject;
      public void setObject(Objec t obj)
      {
      myObject = obj;
      }
      public void useObject(Strin g method)
      {
      // use the method "method" from myObject
      }
      }
      [/CODE]Is that correct? If so, maybe getClass and getDeclaredMeth od might help. (They look like it, but I haven't tested them yet.)

      Greetings,
      Nepomuk

      Comment

      • Nepomuk
        Recognized Expert Specialist
        • Aug 2007
        • 3111

        #4
        Originally posted by madhoriya22
        Hi,
        Collect ur object in object of class Object. Inside method check
        Code:
         
        if(ob instanceOf C1) {
         
        //call C1 class method
        }else if(ob instanceOf C2) {
         
        //call C2 class method
        }else if(ob instanceOf C3) {
         
        //call C3 class method
        }
        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

        Comment

        • s16kumar
          New Member
          • Sep 2007
          • 4

          #5
          Originally posted by madhoriya22
          Hi,
          Collect ur object in object of class Object. Inside method check
          Code:
           
          if(ob instanceOf C1) {
           
          //call C1 class method
          }else if(ob instanceOf C2) {
           
          //call C2 class method
          }else if(ob instanceOf C3) {
           
          //call C3 class method
          }
          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..

          Comment

          • Nepomuk
            Recognized Expert Specialist
            • Aug 2007
            • 3111

            #6
            Originally posted by s16kumar
            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

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

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

              Comment

              • madhoriya22
                Contributor
                • Jul 2007
                • 251

                #8
                Originally posted by nepomuk
                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 :)

                Comment

                • Nepomuk
                  Recognized Expert Specialist
                  • Aug 2007
                  • 3111

                  #9
                  Originally posted by madhoriya22
                  Hi Nepomuk,
                  Nice description buddy .. You are correct! .. My solution was not generalized :)
                  Hi Madhoriya! I do my best! ^^

                  Comment

                  • s16kumar
                    New Member
                    • Sep 2007
                    • 4

                    #10
                    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 getDeclaredMeth od also does not help..

                    Thank you,..

                    Comment

                    • madhoriya22
                      Contributor
                      • Jul 2007
                      • 251

                      #11
                      Originally posted by s16kumar
                      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 getDeclaredMeth od also does not help..

                      Thank you,..
                      Hi,
                      Can show through some relevant code that how you are doing this ?
                      Last edited by madhoriya22; Sep 10 '07, 10:27 AM. Reason: To add my signature :)

                      Comment

                      • r035198x
                        MVP
                        • Sep 2006
                        • 13225

                        #12
                        Use reflection

                        Comment

                        • s16kumar
                          New Member
                          • Sep 2007
                          • 4

                          #13
                          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...

                          Comment

                          • r035198x
                            MVP
                            • Sep 2006
                            • 13225

                            #14
                            Originally posted by s16kumar
                            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.

                            Comment

                            • Nepomuk
                              Recognized Expert Specialist
                              • Aug 2007
                              • 3111

                              #15
                              I've been experimenting and here's a working example:
                              [CODE=java]
                              import java.lang.refle ct.Method;

                              public class C3 {

                              public static void main(String[] args) {
                              C1 c1 = new C1();
                              C2 c2 = new C2();
                              useClass(c1, "doSomethin g");
                              useClass(c2, "doSomethingEls e");
                              useClass(c2, "doSomethingThi rd");
                              }
                              private static void useClass(Object obj, String method)
                              {
                              try
                              {
                              Class params[] = {};
                              Method meth = obj.getClass(). getDeclaredMeth od(method, params);
                              Object paramsObj[] = {};
                              meth.invoke(obj , paramsObj);
                              }
                              catch(NoSuchMet hodException nsme)
                              {
                              System.out.prin tln("No method " + method + " in " + obj.getClass()) ;
                              }
                              catch(Exception e)
                              {
                              e.printStackTra ce();
                              }
                              }
                              }
                              class C1 {
                              void doSomething()
                              {
                              System.out.prin tln("Using C1.doSomething( )");
                              }
                              }
                              class C2 {
                              void doSomethingElse ()
                              {
                              System.out.prin tln("Using C2.doSomethingE lse()");
                              }
                              void doSomethingThir d()
                              {
                              System.out.prin tln("Using C2.doSomethingT hird()");
                              }
                              }
                              [/CODE]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

                              Comment

                              Working...