/** * @author mh, inspired by Mark Lutton, Brookline, NH * @version 1.0 * @date 04.04.2002 * @package informatik2.hello * @class Hello6 */ // richer interfaces increase the chance of reusability; // moreover, several constructors possible; public class Hello6 { public static void main (String[] argv) { Mouth mouth = new SimpleMouth("Hello, World!"); mouth.say(); mouth.open("Hello, Europe!"); mouth.say(); Mouth mund = new CountingMouth(); mund.open("Hallo, Welt!"); mund.say(); mund.open("Hallo, Europa!"); mund.say(); } // main } // Hello6 //--------------------- cut into separate files ----------------------------- interface Mouth { public void say(); public void open(String thought); } // Mouth //--------------------- cut into separate files ----------------------------- class SimpleMouth implements Mouth { public SimpleMouth() { what = null; } // SimpleMouth public SimpleMouth (String thought) { what = thought; } // SimpleMouth public void say() { System.out.println(what); } // say public void open (String thought) { what = thought; } // open private String what; } // SimpleMouth //--------------------- cut into separate files ----------------------------- class CountingMouth implements Mouth { public CountingMouth () { what = null; count = 0; } // CountingMouth public CountingMouth (String thought) { what = thought; count = 0; } // CountingMouth public void say() { count++; System.out.println("(" + count + ") " + what); } // say public void open (String thought) { what = thought; } // open private String what; private int count; } // CountingMouth