001package edu.pdx.cs410J.lang;
002
003/**
004 * This class demonstrate the <code>instanceof</code> operator and the
005 * concept of interfaces using the animal hierarchy.  It uses the
006 * <code>instanceof</code> operator to determine whether or not an
007 * animal implements the <code>Flies</code> interface.
008 */
009public class DoIFly {
010
011  /**
012   * Prints out whether or not an animal can fly.  It checks the
013   * run-time type of the <code>Animal</code>.  If the animal
014   * implements the <code>Flies</code> interface, then the animal can
015   * fly.
016   */
017  private static void doIFly(Animal animal) {
018    boolean iFly = (animal instanceof Flies);
019    System.out.print("Does " + animal.getName() + " fly?  ");
020    System.out.println((iFly ? "Yes." : "No."));
021  }
022
023  /**
024   * This main program creates several animals and then prints out
025   * whether or not they can fly.
026   */
027  public static void main(String[] args) {
028    doIFly(new Cow("Bessy"));
029    doIFly(new Bee("Buzz"));
030    doIFly(new Turkey("Tom"));
031  }
032
033}