// example of using polymorphism with interfaces package interfaceexample; public class RoboMain { // works for all Machines -- Machines, Robots public static void morning(Machine [] employees){ for (Machine box : employees) { box.turnOn(); } } // works for all Fighters -- Robots, Bees, Ninjas public static void fight (Fighter f1, Fighter f2) { f1.attack(); f2.defend(); f2.attack(); f1.defend(); } // works for all Workers -- Robots, Bees public static void workday(Worker ... employees) { for (Worker worker : employees) { worker.doWork(); } } // works for all NoiseMakers -- Robots, Hyenas public static void talk(NoiseMaker a, NoiseMaker b) { a.makeNoise(); b.makeNoise(); } public static void main(String[] args) { System.out.println("\nCreating robot army: "); Robot [] robots = new Robot[10]; for (int i = 0; i < robots.length; i++) { robots[i] = new Robot("Robot#"+(i+1), i*10); } System.out.println("\nThings Robots can do: "); morning(robots); fight(robots[0], robots[9]); workday(robots); talk(robots[5], robots[2]); System.out.println("\nCreating some more stuff: "); Bee b = new Bee(); Ninja n = new Ninja(); Hyena h = new Hyena(15); Robot r = robots[3]; System.out.println("\nFighters: "); fight(r, n); fight(b, n); System.out.println("\nWorkers: "); workday(new Bee(), robots[7], new Bee(), robots[4], new Bee(), new Bee()); System.out.println("\nNoiseMakers: "); talk(r, h); System.out.println("\nHiring an assassin"); n.assassinate(b); // since robots aren't living beings, ninja refuses n.assassinate(robots[3]); n.assassinate(h); // ninja refuses to kill other ninjas too n.assassinate(n); } }