package examples; //Any new Java class can implement any number of interfaces class Comedian implements Laughable, Booable { String name; public Comedian(String s) { name = new String(s); } // Many possible methods omitted public void laugh() { System.out.println("Ha ha ha for " + name); } public void boo() { System.out.println(name + " really stinks!"); } } class SitCom implements Laughable { String title; public SitCom(String s) { title = new String(s); } // Many possible methods omitted public void laugh() { System.out.println("Chuckle chuckle for " + title); } } class Clown implements Laughable, Booable { String type; public Clown(String s) { type = new String(s); } // Many possible methods omitted public void laugh() { System.out.println(type + " clown makes me chortle"); } public void boo() { System.out.println(type + " clown makes me gag!"); } } public class ex24 { public static void main(String [] args) { Laughable L1, L2, L3, L4, L5; // L1 = new Laughable(); // not legal, since we cannot make objects // from the interface itself. We need to use // the classe that implement it. L1 = new Clown("Rodeo"); L1.laugh(); // Even though the method boo() is defined for the class Clown, it // cannot be accessed here, since it is not in the Laughable interface. // The statement below will generate a compilation error. // L1.boo(); L2 = new SitCom("Scrubs"); L2.laugh(); L3 = new Comedian("Chris Rock"); L3.laugh(); L4 = new SitCom("Two and a Half Men"); L4.laugh(); L5 = new Clown("Circus"); L5.laugh(); Booable B1, B2, B3, B4, B5; B1 = (Booable) L1; B1.boo(); // B2 = (Booable) L2; // not legal, since the object currently in L2 // does not implement Booable B3 = (Booable) L3; B3.boo(); // B4 = (Booable) L3; B5 = (Booable) L5; B5.boo(); } }