Way2Java

Retrieve methods of current class and super classes

It is advised to read the notes on Java Reflection API before attempting the programs.

Example on Retrieve methods of class
import java.lang.reflect.Method;
class Test
{
  public void display()  {   }
  protected void exhibit()  {   }
}
public class Demo extends Test
{
  public void show()  {   }
  protected void calculate()  {   }
  public static void main(String args[])
  {
    Demo d1 = new Demo();
    Class c = d1.getClass();  

    System.out.println("public methods of current class and super classes:" ) ;
    Method array1[] = c.getMethods();
    for(int i = 0 ; i < array1.length; i++)
    {
      System.out.println(array1[i].getName());
    }

    System.out.println("\nAll methods of current class of any specifier:") ;
    Method array2[ ] = c.getDeclaredMethods();
    for( int i = 0; i < array2.length; i++)                        
    {
      System.out.println(array2[i].getName()); 
    }
  }
}

getMethods() returns only public constructors of current class and all super classes.

getDeclaredMethods() returns all constructors of any specifier of current class