Extending an interface with inheritance : Inheritance Composition « Class « Java






Extending an interface with inheritance

// : c08:HorrorShow.java
// Extending an interface with inheritance.
// From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002
// www.BruceEckel.com. See copyright notice in CopyRight.txt.

interface Monster {
  void menace();
}

interface DangerousMonster extends Monster {
  void destroy();
}

interface Lethal {
  void kill();
}

class DragonZilla implements DangerousMonster {
  public void menace() {
  }

  public void destroy() {
  }
}

interface Vampire extends DangerousMonster, Lethal {
  void drinkBlood();
}

class VeryBadVampire implements Vampire {
  public void menace() {
  }

  public void destroy() {
  }

  public void kill() {
  }

  public void drinkBlood() {
  }
}

public class HorrorShow {
  static void u(Monster b) {
    b.menace();
  }

  static void v(DangerousMonster d) {
    d.menace();
    d.destroy();
  }

  static void w(Lethal l) {
    l.kill();
  }

  public static void main(String[] args) {
    DangerousMonster barney = new DragonZilla();
    u(barney);
    v(barney);
    Vampire vlad = new VeryBadVampire();
    u(vlad);
    v(vlad);
    w(vlad);
  }
} ///:~



           
       








Related examples in the same category

1.Combining composition and inheritanceCombining composition and inheritance
2.Inheritance, constructors and argumentsInheritance, constructors and arguments
3.Inheritance syntax and propertiesInheritance syntax and properties
4.Composition for code reuseComposition for code reuse
5.Cleanup and inheritanceCleanup and inheritance
6.Proper inheritance of an inner classProper inheritance of an inner class
7.Inheriting an inner class