edu.pdx.cs.joy.lang.DoIFly Maven / Gradle / Ivy
The newest version!
package edu.pdx.cs.joy.lang;
/**
* This class demonstrate the instanceof
operator and the
* concept of interfaces using the animal hierarchy. It uses the
* instanceof
operator to determine whether or not an
* animal implements the Flies
interface.
*/
public class DoIFly {
/**
* Prints out whether or not an animal can fly. It checks the
* run-time type of the Animal
. If the animal
* implements the Flies
interface, then the animal can
* fly.
*/
private static void doIFly(Animal animal) {
boolean iFly = (animal instanceof Flies);
System.out.print("Does " + animal.getName() + " fly? ");
System.out.println((iFly ? "Yes." : "No."));
}
/**
* This main program creates several animals and then prints out
* whether or not they can fly.
*/
public static void main(String[] args) {
doIFly(new Cow("Bessy"));
doIFly(new Bee("Buzz"));
doIFly(new Turkey("Tom"));
}
}