title: How to list methods that are not inherited?
body: <p>I have the following two Java classes (Command and Player). I would like to be able to list from the Command class all the public methods from the Player class, without having to hard-code them. I tried several ways, including an interface, or the command pattern, but I am not fluent in Java and I did not manage to make them work.</p>
  
  <p>It seems to me that using Java's reflection API would be easier. However, when I use it to print the public methods from the Player class, I get extraneous methods, which I assume were inherited from the Object class.</p>
  
  <p>Is there a way to only include those methods I defined myself (i.e. those starting with "public method: public void Player.")?</p>
  
  <p>Thank you,</p>
  
  <p>LC</p>
  
  <p>Here is the output I get:</p>
  
  <pre><code>public method: public void Player.search(Command)
  public method: public Room Player.getCurrentRoom()
  public method: public void Player.engage()
  public method: public void Player.trade(Command)
  public method: public void Player.goRoom(Command)
  public method: public void Player.takeItem(Command)
  public method: public void Player.dropItem(Command)
  public method: public void Player.lock(Command)
  public method: public void Player.unlock(Command)
  public method: public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
  public method: public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
  public method: public final void java.lang.Object.wait() throws java.lang.InterruptedException
  public method: public boolean java.lang.Object.equals(java.lang.Object)
  public method: public java.lang.String java.lang.Object.toString()
  public method: public native int java.lang.Object.hashCode()
  public method: public final native java.lang.Class java.lang.Object.getClass()
  public method: public final native void java.lang.Object.notify()
  public method: public final native void java.lang.Object.notifyAll()
  </code></pre>
  
  <p>Here is the Command class:</p>
  
  <pre><code>import java.lang.reflect.Method;
  
  public class Command
  {
  
      // a constant array that holds all valid command words
      private static String[] validCommands;      
  
      private String commandWord;
      private String secondWord;
  
      public Command(String firstWord, String secondWord)
      {
          commandWord = firstWord;
          this.secondWord = secondWord;
      }
  
          [...] //some code omitted
  
      public boolean process(Player player) 
      {
  
          Class pClass = player.getClass();
          Method[] methods = pClass.getMethods();
          for (int i = 0; i &lt; methods.length; i++) {
              System.out.println("public method: " + methods[i]);
          }
  
          boolean wantToQuit = false;
  
          if(commandWord == null) {
              System.out.println("I don't know what you mean...");
              return false;
          }
  
          if (commandWord.equals("help")) {
              printHelp();
          }
          else if (commandWord.equals("go")) {
              player.goRoom(this);
          }
          else if (commandWord.equals("quit")) {
              wantToQuit = quit();
          }
          else if (commandWord.equals("take")) {
              player.takeItem(this);
          }
          else if (commandWord.equals("drop")) {
              player.dropItem(this);
          }
          else if (commandWord.equals("search")) {
              player.search(this);
          }
          else if (commandWord.equals("engage")) {
              player.engage();
          }
          else if (commandWord.equals("trade")) {
              player.trade(this);
          }
          else if (commandWord.equals("lock")) {
              player.lock(this);
          }
          else if (commandWord.equals("unlock")) {
              player.unlock(this);
          }
          else {
              System.out.println("Invalid command. Type 'help' if you forgot the list of available commands.");
          }
          // else command not recognised.
          return wantToQuit;
      }
  }
  </code></pre>
  
  <p>Here is the outline of the Player class:</p>
  
  <pre><code>public class Player
  {
      private String name;
      private Room currentRoom;
      private ArrayList&lt;Item&gt; items;
  
      Player (String name, Room startingRoom)
      {
          this.name = name;
          items = new ArrayList&lt;Item&gt;();
          this.currentRoom = startingRoom;
          printWelcome();
      }
  
      public void engage()
      {
          [...]
      }
  
      public void trade(Command command) 
      {
          [...]       }
  
      public void goRoom(Command command) 
      {   
          [...]       }
  
      public void search(Command command) 
      {
          [...]       }
  
      public void takeItem(Command command) 
      {
          [...]       }
  
      public void dropItem(Command command) 
      {
          [...]       }
  
      public void lock(Command command)
      {   
          [...]       }
  
      public void unlock(Command command)
      {   
          [...]
      }
  
  }
  </code></pre>