title: Why is "instanceof" not working?
body: <p>I'm using the Java <em>instanceof</em> but it doesn't seem to be working.</p>
  
  <p>I have <strong>three</strong> java classes that extend a Hero class.<br>
  The <strong>Hero.java</strong> class:</p>
  
  <pre><code>public abstract class Hero {
  
      protected int health;
  
      public Hero() { 
      }
  }
  </code></pre>
  
  <p>The other <strong>three</strong> classes:</p>
  
  <pre><code>public class Archer extends Hero {
      public Archer() {
      }
  }
  
  public class Mage extends Hero {
      public Mage() {
      }
  }
  
  public class Warrior extends Hero {
      public Warrior() {
      }
  }
  </code></pre>
  
  <p>I have this main class <strong>WelcomeScreen.java</strong></p>
  
  <pre><code>public class WelcomeScreen {
  
      private Archer archer;
      private Mage mage;
      private Warrior warrior;
      private Hero hero;
  
  public WelcomeScreen() {
  
          // choose a hero (archer/mage/warrior)
          hero = archer;
          new Game(hero);
      }
  
      public static void main(String args[]) {
          new WelcomeScreen();
      }
  
  }
  </code></pre>
  
  <p>that instantiates the <strong>Game.java</strong> class</p>
  
  <pre><code>public class Game {
  
      public Game(Hero chosenHero) {
  
          if (chosenHero instanceof Mage) {
              System.out.println("you selected mage");
          } else if (chosenHero instanceof Archer) {
              System.out.println("you selected archer");
          } else if (chosenHero instanceof Warrior) {
              System.out.println("you selected warrior");
          } else {
              System.out.println("you selected NOTHING");
          }
      }
  
  }
  </code></pre>
  
  <p>In <strong>Game.java</strong>, the code is meant to check whether chosenHero is an object of <strong>Archer.java</strong>, <strong>Warrior.java</strong>, or <strong>Mage.java</strong>, but I result with <em>"you selected NOTHING"</em>. Why does <em>instanceof</em> fail to check if I already assigned it to <strong>Archer.java</strong> in the WelcomeScreen?</p>