// class representing a Ninja // example of implementing an interface // and what happens when example classes are written after 1 am package Strategy; public class Ninja implements Fighter { // constructors public 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."); } // fake 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."); } // prepare for fighting // for interface Fighter // overriding defender method public void prepareForFight() { Fighter.super.prepareForFight(); System.out.println("The ninja is always prepared"); } // ninjas will assassinate anything except other ninjas 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; } }