// class representing a Ninja // example of implementing an interface package genex; public class Ninja extends Namey implements Fighter{ // constructors public Ninja() { super("Ninja"); // constructors shouldn't print things . . . except for ninja constructors // i make my own fun. System.out.println("A ninja was created. This is the last warning you will ever have."); } public Ninja(String n) { super(n); } // accessors and mutators // pretend accessor public boolean getVisible() { // ninjas are never visible, fool return false; } // pretend mutator public void setVisible(boolean visible) { // you cannot trick the ninja that way } //methods // attack // for interface Fighter public void attack() { System.out.println("The ninja killed you earlier, when you weren't watching. Have a nice day."); } // defend self // for interface Fighter public void defend() { System.out.println("The ninja is safe. You can't hit what you can't see."); } // ninjas will assassinate anything alive // example of using marker interface to tell appropriate behavior public void assassinate(Object victim){ System.out.println(victim + " will never see the ninja coming."); if (victim instanceof Ninja) { System.out.println("Professional courtesy forbids me to kill other ninjas."); } else { attack(); } } // utilities @Override public String toString() { // ninjas are invisible even to println return ""; } @Override // you can't compare a ninja to anything else // you'd have to catch him first. public boolean equals(Object obj) { return false; } }