/** * @author monika heiner * @version 1.0 * @date 04.04.2002 * @package informatik2.hello * @class Hello7 */ // avoiding of code duplication by just noting the add-on's, // -> inheritance, OO basic principle: // the heir inherits all features of all ancestors; // inherited functionality may be changed -> polymorphy; public class Hello7 { public static void main (String[] argv) { Mouth mouth = new Mouth("Hello, World!"); mouth.say(); mouth.open("Hello, Europe!"); mouth.say(); CountingMouth mund = new CountingMouth(); mund.open("Hallo, Welt!"); mund.say(); mund.open("Hallo, Europa!"); mund.say(); } // main } // Hello7 //--------------------- cut into separate files ----------------------------- class Mouth { public Mouth () { what = null; } // Mouth public Mouth (String thought) { what = thought; } // Mouth public void say() { System.out.println(what); } // say public void open (String thought) { what = thought; } // open private String what; } // Mouth //--------------------- cut into separate files ----------------------------- class CountingMouth extends Mouth { public CountingMouth () { super(); count = 0; } // CountingMouth public CountingMouth (String thought) { super(thought); count = 0; } // CountingMouth public void say() { count++; System.out.print("(" + count + ") "); super.say(); } // say private int count; } // CountingMouth