Results 1 to 4 of 4
  1. #1
    joeyvitoro is offline Member
    Join Date
    Oct 2011
    Posts
    90
    Rep Power
    0

    Default Dynamically call inherited method

    Hi, I have an abstract super class and several sub classes that inherit from it. I need to dynamically pick which class AND which method I use at runtime. Unfortunately I am getting a java.lang.NoSuchMethodException: TestChildClass.getI(). As TestChildClass is a child of TestClass and TestClass.getI() does exist, I assume I may need something else. Thanks!
    Java Code:
    import java.lang.reflect.Method;
    public class TestClass 
    {
    	protected static int i;
    	
    	public static int getDynamicMethod(String className, String methodName)
    	{
    		try
    		{
    			Class messageClass = Class.forName(className);
    			Method classMethod = messageClass.getDeclaredMethod(methodName, null);
    			return (Integer) classMethod.invoke(null);
    		}
    		catch(Exception ex)
    		{
    			ex.printStackTrace();
    		}
    		return -1;
    	}
    	public static int getI()
    	{
    		return i;
    	}
    	public static void main(String[] args) 
    	{	
    		int testI = TestClass.getDynamicMethod("TestChildClass", "getI");
    		System.out.println(testI);
    	}
    }
    
    class TestChildClass extends TestClass
    {
    	public TestChildClass() 
    	{
    		i=0;
    	}
    }
    Last edited by joeyvitoro; 12-02-2011 at 06:13 PM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default Re: Dynamically call inherited method

    If the methods aren't static, the first parameter of the invoke( ... ) call can never be null. If the method doesn't take arguments, leave out the second null parameter.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    joeyvitoro is offline Member
    Join Date
    Oct 2011
    Posts
    90
    Rep Power
    0

    Default Re: Dynamically call inherited method

    Jos,

    Thanks for the reply. I have made these changes and I still get the same error.

  4. #4
    joeyvitoro is offline Member
    Join Date
    Oct 2011
    Posts
    90
    Rep Power
    0

    Default Re: Dynamically call inherited method

    SOLUTION

    getDeclaredMethod("methodName") will not work for an inherited method. However, getMethod("methodName") will.

Similar Threads

  1. Inherited actionPerformed method
    By doobybug in forum New To Java
    Replies: 4
    Last Post: 08-13-2011, 11:33 AM
  2. Dynamically create objects, set value and call
    By buntyindia in forum New To Java
    Replies: 5
    Last Post: 05-25-2011, 05:59 PM
  3. Inherited method that returns string
    By Xeal Rebad in forum New To Java
    Replies: 5
    Last Post: 05-23-2011, 01:26 PM
  4. Inherited method returning bad value
    By viking90 in forum New To Java
    Replies: 11
    Last Post: 04-07-2010, 03:53 PM
  5. Replies: 0
    Last Post: 02-17-2008, 08:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •