{"id":"class-diagrams-inheritance","slug":"inheritance","title":"Inheritance and abstract classes","text":"### A triangle points to the parent\n\nInheritance (module 34) has a line of its own in a class diagram. The recycling center's glass bins are sorted by color, so a `GlassBin` extends `Bin` with a color:\n\n\n<figure class=\"uml\"><div class=\"uml-scroll\" tabindex=\"0\" role=\"group\" aria-label=\"Class diagram of Bin and GlassBin\">\n<svg class=\"uml-svg\" viewBox=\"0 0 309 280\" width=\"309\" height=\"280\" style=\"width:19.32rem;min-width:17.71rem;aspect-ratio:309/280\" font-size=\"12\" aria-hidden=\"true\" focusable=\"false\"><path class=\"u-edge\" fill=\"none\" d=\"M154.5 153L154.5 186\"/><path class=\"u-tri\" d=\"M154.5 142L147.5 153L161.5 153Z\"/><rect class=\"u-bg\" fill=\"none\" x=\"13\" y=\"6\" width=\"283\" height=\"136\"/><rect class=\"u-head\" fill=\"none\" x=\"13\" y=\"6\" width=\"283\" height=\"24\"/><text x=\"154.5\" y=\"22\" text-anchor=\"middle\" class=\"u-name\">Bin</text><text x=\"21\" y=\"46\">-material: String</text><text x=\"21\" y=\"62\">-capacity: int</text><text x=\"21\" y=\"78\">-load: int</text><text x=\"21\" y=\"102\">+Bin(material: String, capacity: int)</text><text x=\"21\" y=\"118\">+add(kilos: int): boolean</text><text x=\"21\" y=\"134\">+toString(): String</text><path class=\"u-sep\" fill=\"none\" d=\"M13 30h283M13 86h283\"/><rect class=\"u-frame\" fill=\"none\" x=\"13\" y=\"6\" width=\"283\" height=\"136\"/><rect class=\"u-bg\" fill=\"none\" x=\"6\" y=\"186\" width=\"297\" height=\"88\"/><rect class=\"u-head\" fill=\"none\" x=\"6\" y=\"186\" width=\"297\" height=\"24\"/><text x=\"154.5\" y=\"202\" text-anchor=\"middle\" class=\"u-name\">GlassBin</text><text x=\"14\" y=\"226\">-color: String</text><text x=\"14\" y=\"250\">+GlassBin(color: String, capacity: int)</text><text x=\"14\" y=\"266\">+toString(): String</text><path class=\"u-sep\" fill=\"none\" d=\"M6 210h297M6 234h297\"/><rect class=\"u-frame\" fill=\"none\" x=\"6\" y=\"186\" width=\"297\" height=\"88\"/></svg>\n</div><details class=\"uml-text\"><summary>The diagram as text</summary><ul><li><strong>Bin</strong>, a class<ul><li>private field <code>material: String</code></li><li>private field <code>capacity: int</code></li><li>private field <code>load: int</code></li><li>public constructor <code>Bin(material: String, capacity: int)</code></li><li>public method <code>add(kilos: int): boolean</code></li><li>public method <code>toString(): String</code></li></ul></li><li><strong>GlassBin</strong>, a class that extends Bin<ul><li>private field <code>color: String</code></li><li>public constructor <code>GlassBin(color: String, capacity: int)</code></li><li>public method <code>toString(): String</code></li></ul></li></ul></details></figure>\n\n\n- The subclass's box is below its parent's, joined to it by a line with a **hollow triangle** at the parent's end. The triangle points to the class that is extended: GlassBin extends Bin.\n- The subclass's box shows only what GlassBin itself writes: its own field, its constructor, and the `toString` it overrides. It inherits `add`, but `add` is drawn once, in Bin's box.\n- Bin's private fields aren't repeated in GlassBin's box either. They're declared in Bin, even though every GlassBin object has them.\n\n```java\npublic class Main {\n    public static void main(String[] args) {\n        GlassBin green = new GlassBin(\"green\", 400);\n        green.add(120);\n        System.out.println(green);\n    }\n}\n// ==== Bin.java ====\npublic class Bin {\n    private String material;\n    private int capacity;\n    private int load;\n\n    public Bin(String material, int capacity) {\n        this.material = material;\n        this.capacity = capacity;\n        this.load = 0;\n    }\n\n    public boolean add(int kilos) {\n        if (this.load + kilos > this.capacity) {\n            return false;\n        }\n        this.load += kilos;\n        return true;\n    }\n\n    @Override\n    public String toString() {\n        return this.material + \": \" + this.load + \"/\" + this.capacity + \" kg\";\n    }\n}\n// ==== GlassBin.java ====\npublic class GlassBin extends Bin {\n    private String color;\n\n    public GlassBin(String color, int capacity) {\n        super(\"glass\", capacity);\n        this.color = color;\n    }\n\n    @Override\n    public String toString() {\n        return this.color + \" \" + super.toString();\n    }\n}\n```\n\n```output\ngreen glass: 120/400 kg\n```\n\nWhen several classes extend one parent, the children sit side by side under it, and their lines meet at the parent's one triangle.\n\n### Abstract classes\n\nAn abstract class (module 34) is drawn like any other class, with two marks: «abstract» above its name, which is in italics, and its abstract methods in italics too.\n\nPeople also bring the recycling center things that don't go in a bin, and pay a fee that depends on what they bring:\n\n\n<figure class=\"uml\"><div class=\"uml-scroll\" tabindex=\"0\" role=\"group\" aria-label=\"Class diagram of DropOff, Furniture and Electronics\">\n<svg class=\"uml-svg\" viewBox=\"0 0 529 282\" width=\"529\" height=\"282\" style=\"width:33.07rem;min-width:30.31rem;aspect-ratio:529/282\" font-size=\"12\" aria-hidden=\"true\" focusable=\"false\"><path class=\"u-edge\" fill=\"none\" d=\"M230.3 153L230.3 164L93.5 164L93.5 188\"/><path class=\"u-edge\" fill=\"none\" d=\"M230.3 153L230.3 164L367 164L367 188\"/><path class=\"u-tri\" d=\"M230.3 142L223.3 153L237.3 153Z\"/><rect class=\"u-bg\" fill=\"none\" x=\"150.3\" y=\"6\" width=\"160\" height=\"136\"/><rect class=\"u-head\" fill=\"none\" x=\"150.3\" y=\"6\" width=\"160\" height=\"40\"/><text x=\"230.3\" y=\"22\" text-anchor=\"middle\" class=\"u-st\">«abstract»</text><text x=\"230.3\" y=\"38\" text-anchor=\"middle\" class=\"u-name u-it\">DropOff</text><text x=\"158.3\" y=\"62\">-kilos: int</text><text x=\"158.3\" y=\"86\">+DropOff(kilos: int)</text><text x=\"158.3\" y=\"102\">+getKilos(): int</text><text x=\"158.3\" y=\"118\" class=\"u-it\">+fee(): int</text><text x=\"158.3\" y=\"134\">+toString(): String</text><path class=\"u-sep\" fill=\"none\" d=\"M150.3 46h160M150.3 70h160\"/><rect class=\"u-frame\" fill=\"none\" x=\"150.3\" y=\"6\" width=\"160\" height=\"136\"/><rect class=\"u-bg\" fill=\"none\" x=\"6\" y=\"188\" width=\"175\" height=\"72\"/><rect class=\"u-head\" fill=\"none\" x=\"6\" y=\"188\" width=\"175\" height=\"24\"/><text x=\"93.5\" y=\"204\" text-anchor=\"middle\" class=\"u-name\">Furniture</text><text x=\"14\" y=\"236\">+Furniture(kilos: int)</text><text x=\"14\" y=\"252\">+fee(): int</text><path class=\"u-sep\" fill=\"none\" d=\"M6 212h175M6 220h175\"/><rect class=\"u-frame\" fill=\"none\" x=\"6\" y=\"188\" width=\"175\" height=\"72\"/><rect class=\"u-bg\" fill=\"none\" x=\"211\" y=\"188\" width=\"312\" height=\"88\"/><rect class=\"u-head\" fill=\"none\" x=\"211\" y=\"188\" width=\"312\" height=\"24\"/><text x=\"367\" y=\"204\" text-anchor=\"middle\" class=\"u-name\">Electronics</text><text x=\"219\" y=\"228\">-screen: boolean</text><text x=\"219\" y=\"252\">+Electronics(kilos: int, screen: boolean)</text><text x=\"219\" y=\"268\">+fee(): int</text><path class=\"u-sep\" fill=\"none\" d=\"M211 212h312M211 236h312\"/><rect class=\"u-frame\" fill=\"none\" x=\"211\" y=\"188\" width=\"312\" height=\"88\"/></svg>\n</div><details class=\"uml-text\"><summary>The diagram as text</summary><ul><li><strong>DropOff</strong>, an abstract class<ul><li>private field <code>kilos: int</code></li><li>public constructor <code>DropOff(kilos: int)</code></li><li>public method <code>getKilos(): int</code></li><li>public abstract method <code>fee(): int</code></li><li>public method <code>toString(): String</code></li></ul></li><li><strong>Furniture</strong>, a class that extends DropOff<ul><li>public constructor <code>Furniture(kilos: int)</code></li><li>public method <code>fee(): int</code></li></ul></li><li><strong>Electronics</strong>, a class that extends DropOff<ul><li>private field <code>screen: boolean</code></li><li>public constructor <code>Electronics(kilos: int, screen: boolean)</code></li><li>public method <code>fee(): int</code></li></ul></li></ul></details></figure>\n\n\n- DropOff's name is in italics under «abstract»: there are no objects of DropOff itself, only Furniture and Electronics objects.\n- `fee()` is in italics in DropOff's box: it's abstract there, a header without a body. Each subclass writes it, so it's in their boxes again, in plain letters.\n- A subclass can have no fields of its own, like Furniture, whose field part is empty.\n\n```java\nimport java.util.ArrayList;\n\npublic class Main {\n    public static void main(String[] args) {\n        ArrayList<DropOff> today = new ArrayList<>();\n        today.add(new Furniture(30));\n        today.add(new Electronics(8, true));\n        today.add(new Electronics(2, false));\n        int total = 0;\n        for (DropOff item : today) {\n            System.out.println(item);\n            total += item.fee();\n        }\n        System.out.println(\"Total \" + total);\n    }\n}\n// ==== DropOff.java ====\npublic abstract class DropOff {\n    private int kilos;\n\n    public DropOff(int kilos) {\n        this.kilos = kilos;\n    }\n\n    public int getKilos() {\n        return this.kilos;\n    }\n\n    public abstract int fee();\n\n    @Override\n    public String toString() {\n        return this.kilos + \" kg, fee \" + this.fee();\n    }\n}\n// ==== Furniture.java ====\npublic class Furniture extends DropOff {\n    public Furniture(int kilos) {\n        super(kilos);\n    }\n\n    @Override\n    public int fee() {\n        return 2 * this.getKilos();\n    }\n}\n// ==== Electronics.java ====\npublic class Electronics extends DropOff {\n    private boolean screen;\n\n    public Electronics(int kilos, boolean screen) {\n        super(kilos);\n        this.screen = screen;\n    }\n\n    @Override\n    public int fee() {\n        if (this.screen) {\n            return 20;\n        }\n        return 5;\n    }\n}\n```\n\n```output\n30 kg, fee 60\n8 kg, fee 20\n2 kg, fee 5\nTotal 85\n```\n","task":"Old tires are a drop-off too. `DropOff.java` is ready. Write the class `Tires` in `Tires.java`, as the diagram shows. Tires cost 4 dollars each.\n\n\n<figure class=\"uml\"><div class=\"uml-scroll\" tabindex=\"0\" role=\"group\" aria-label=\"Class diagram of DropOff and Tires\">\n<svg class=\"uml-svg\" viewBox=\"0 0 244 280\" width=\"244\" height=\"280\" style=\"width:15.25rem;min-width:13.98rem;aspect-ratio:244/280\" font-size=\"12\" aria-hidden=\"true\" focusable=\"false\"><path class=\"u-edge\" fill=\"none\" d=\"M122 153L122 186\"/><path class=\"u-tri\" d=\"M122 142L115 153L129 153Z\"/><rect class=\"u-bg\" fill=\"none\" x=\"42\" y=\"6\" width=\"160\" height=\"136\"/><rect class=\"u-head\" fill=\"none\" x=\"42\" y=\"6\" width=\"160\" height=\"40\"/><text x=\"122\" y=\"22\" text-anchor=\"middle\" class=\"u-st\">«abstract»</text><text x=\"122\" y=\"38\" text-anchor=\"middle\" class=\"u-name u-it\">DropOff</text><text x=\"50\" y=\"62\">-kilos: int</text><text x=\"50\" y=\"86\">+DropOff(kilos: int)</text><text x=\"50\" y=\"102\">+getKilos(): int</text><text x=\"50\" y=\"118\" class=\"u-it\">+fee(): int</text><text x=\"50\" y=\"134\">+toString(): String</text><path class=\"u-sep\" fill=\"none\" d=\"M42 46h160M42 70h160\"/><rect class=\"u-frame\" fill=\"none\" x=\"42\" y=\"6\" width=\"160\" height=\"136\"/><rect class=\"u-bg\" fill=\"none\" x=\"6\" y=\"186\" width=\"232\" height=\"88\"/><rect class=\"u-head\" fill=\"none\" x=\"6\" y=\"186\" width=\"232\" height=\"24\"/><text x=\"122\" y=\"202\" text-anchor=\"middle\" class=\"u-name\">Tires</text><text x=\"14\" y=\"226\">-count: int</text><text x=\"14\" y=\"250\">+Tires(kilos: int, count: int)</text><text x=\"14\" y=\"266\">+fee(): int</text><path class=\"u-sep\" fill=\"none\" d=\"M6 210h232M6 234h232\"/><rect class=\"u-frame\" fill=\"none\" x=\"6\" y=\"186\" width=\"232\" height=\"88\"/></svg>\n</div><details class=\"uml-text\"><summary>The diagram as text</summary><ul><li><strong>DropOff</strong>, an abstract class<ul><li>private field <code>kilos: int</code></li><li>public constructor <code>DropOff(kilos: int)</code></li><li>public method <code>getKilos(): int</code></li><li>public abstract method <code>fee(): int</code></li><li>public method <code>toString(): String</code></li></ul></li><li><strong>Tires</strong>, a class that extends DropOff<ul><li>private field <code>count: int</code></li><li>public constructor <code>Tires(kilos: int, count: int)</code></li><li>public method <code>fee(): int</code></li></ul></li></ul></details></figure>\n\n\nThe check runs:\n\n```java\nDropOff tires = new Tires(36, 4);\nSystem.out.println(tires);\nSystem.out.println(tires.getKilos() + \" \" + tires.fee());\n```\n\nand expects:\n\n```\n36 kg, fee 16\n36 16\n```\n","kind":"code","seed":"// ==== Tires.java ====\npublic class Tires {\n\n}\n\n// ==== DropOff.java ====\npublic abstract class DropOff {\n    private int kilos;\n\n    public DropOff(int kilos) {\n        this.kilos = kilos;\n    }\n\n    public int getKilos() {\n        return this.kilos;\n    }\n\n    public abstract int fee();\n\n    @Override\n    public String toString() {\n        return this.kilos + \" kg, fee \" + this.fee();\n    }\n}\n\n// ==== Main.java ====\npublic class Main {\n    public static void main(String[] args) {\n        DropOff tires = new Tires(36, 4);\n        System.out.println(tires);\n        System.out.println(tires.getKilos() + \" \" + tires.fee());\n    }\n}\n","solution":"// ==== Tires.java ====\npublic class Tires extends DropOff {\n    private int count;\n\n    public Tires(int kilos, int count) {\n        super(kilos);\n        this.count = count;\n    }\n\n    @Override\n    public int fee() {\n        return 4 * this.count;\n    }\n}\n\n// ==== DropOff.java ====\npublic abstract class DropOff {\n    private int kilos;\n\n    public DropOff(int kilos) {\n        this.kilos = kilos;\n    }\n\n    public int getKilos() {\n        return this.kilos;\n    }\n\n    public abstract int fee();\n\n    @Override\n    public String toString() {\n        return this.kilos + \" kg, fee \" + this.fee();\n    }\n}\n\n// ==== Main.java ====\npublic class Main {\n    public static void main(String[] args) {\n        DropOff tires = new Tires(36, 4);\n        System.out.println(tires);\n        System.out.println(tires.getKilos() + \" \" + tires.fee());\n    }\n}\n","hints":["The triangle means `public class Tires extends DropOff`. Tires's own field is `count`. The kilos belong to DropOff, so the constructor passes them on with `super(kilos);`.","`fee()` is abstract in DropOff, so Tires must write it: `public int fee()`, returning 4 times the count. `toString` and `getKilos` are inherited."],"tests":[{"name":"Test 1","stdin":"","call":"DropOff tires = new Tires(36, 4);\nSystem.out.println(tires);\nSystem.out.println(tires.getKilos() + \" \" + tires.fee());\n","expect":"36 kg, fee 16\n36 16","hidden":false},{"name":"Test 2","stdin":"","call":"Tires one = new Tires(9, 1);\nTires stack = new Tires(80, 10);\nSystem.out.println(one + \" / \" + stack);\nSystem.out.println(one.fee() + stack.fee());\n","expect":"9 kg, fee 4 / 80 kg, fee 40\n44","hidden":true}],"require":[{"pattern":"\\bclass\\s+Tires\\s+extends\\s+DropOff\\b","message":"The triangle from Tires to DropOff means `public class Tires extends DropOff`."}],"forbid":[],"more":[{"task":"A music festival's program has this diagram, but the code doesn't match it in two places, and `Main.java` doesn't compile:\n\n\n<figure class=\"uml\"><div class=\"uml-scroll\" tabindex=\"0\" role=\"group\" aria-label=\"Class diagram of Performer, Band and Dj\">\n<svg class=\"uml-svg\" viewBox=\"0 0 449 282\" width=\"449\" height=\"282\" style=\"width:28.07rem;min-width:25.73rem;aspect-ratio:449/282\" font-size=\"12\" aria-hidden=\"true\" focusable=\"false\"><path class=\"u-edge\" fill=\"none\" d=\"M249.8 153L249.8 164L133 164L133 188\"/><path class=\"u-edge\" fill=\"none\" d=\"M249.8 153L249.8 164L366.5 164L366.5 188\"/><path class=\"u-tri\" d=\"M249.8 142L242.8 153L256.8 153Z\"/><rect class=\"u-bg\" fill=\"none\" x=\"155.3\" y=\"6\" width=\"189\" height=\"136\"/><rect class=\"u-head\" fill=\"none\" x=\"155.3\" y=\"6\" width=\"189\" height=\"40\"/><text x=\"249.8\" y=\"22\" text-anchor=\"middle\" class=\"u-st\">«abstract»</text><text x=\"249.8\" y=\"38\" text-anchor=\"middle\" class=\"u-name u-it\">Performer</text><text x=\"163.3\" y=\"62\">-name: String</text><text x=\"163.3\" y=\"86\">+Performer(name: String)</text><text x=\"163.3\" y=\"102\">+getName(): String</text><text x=\"163.3\" y=\"118\" class=\"u-it\">+slotMinutes(): int</text><text x=\"163.3\" y=\"134\">+toString(): String</text><path class=\"u-sep\" fill=\"none\" d=\"M155.3 46h189M155.3 70h189\"/><rect class=\"u-frame\" fill=\"none\" x=\"155.3\" y=\"6\" width=\"189\" height=\"136\"/><rect class=\"u-bg\" fill=\"none\" x=\"6\" y=\"188\" width=\"254\" height=\"88\"/><rect class=\"u-head\" fill=\"none\" x=\"6\" y=\"188\" width=\"254\" height=\"24\"/><text x=\"133\" y=\"204\" text-anchor=\"middle\" class=\"u-name\">Band</text><text x=\"14\" y=\"228\">-members: int</text><text x=\"14\" y=\"252\">+Band(name: String, members: int)</text><text x=\"14\" y=\"268\">+slotMinutes(): int</text><path class=\"u-sep\" fill=\"none\" d=\"M6 212h254M6 236h254\"/><rect class=\"u-frame\" fill=\"none\" x=\"6\" y=\"188\" width=\"254\" height=\"88\"/><rect class=\"u-bg\" fill=\"none\" x=\"290\" y=\"188\" width=\"153\" height=\"72\"/><rect class=\"u-head\" fill=\"none\" x=\"290\" y=\"188\" width=\"153\" height=\"24\"/><text x=\"366.5\" y=\"204\" text-anchor=\"middle\" class=\"u-name\">Dj</text><text x=\"298\" y=\"236\">+Dj(name: String)</text><text x=\"298\" y=\"252\">+slotMinutes(): int</text><path class=\"u-sep\" fill=\"none\" d=\"M290 212h153M290 220h153\"/><rect class=\"u-frame\" fill=\"none\" x=\"290\" y=\"188\" width=\"153\" height=\"72\"/></svg>\n</div><details class=\"uml-text\"><summary>The diagram as text</summary><ul><li><strong>Performer</strong>, an abstract class<ul><li>private field <code>name: String</code></li><li>public constructor <code>Performer(name: String)</code></li><li>public method <code>getName(): String</code></li><li>public abstract method <code>slotMinutes(): int</code></li><li>public method <code>toString(): String</code></li></ul></li><li><strong>Band</strong>, a class that extends Performer<ul><li>private field <code>members: int</code></li><li>public constructor <code>Band(name: String, members: int)</code></li><li>public method <code>slotMinutes(): int</code></li></ul></li><li><strong>Dj</strong>, a class that extends Performer<ul><li>public constructor <code>Dj(name: String)</code></li><li>public method <code>slotMinutes(): int</code></li></ul></li></ul></details></figure>\n\n\nA band plays 30 minutes plus 5 for each member, and a DJ plays 60 minutes. Fix `Performer.java` and `Dj.java` so that they match the diagram, and leave `Main.java` and `Band.java` as they are. The program should print:\n\n```\nThe Low Tides, 50 min\nKora, 60 min\n110 min in all\n```\n","kind":"code","seed":"// ==== Performer.java ====\npublic class Performer {\n    private String name;\n\n    public Performer(String name) {\n        this.name = name;\n    }\n\n    public String getName() {\n        return this.name;\n    }\n\n    public int slotMinutes() {\n        return 0;\n    }\n\n    @Override\n    public String toString() {\n        return this.name + \", \" + this.slotMinutes() + \" min\";\n    }\n}\n\n// ==== Dj.java ====\npublic class Dj {\n    private String name;\n\n    public Dj(String name) {\n        this.name = name;\n    }\n\n    public int slotMinutes() {\n        return 60;\n    }\n}\n\n// ==== Band.java ====\npublic class Band extends Performer {\n    private int members;\n\n    public Band(String name, int members) {\n        super(name);\n        this.members = members;\n    }\n\n    @Override\n    public int slotMinutes() {\n        return 30 + 5 * this.members;\n    }\n}\n\n// ==== Main.java ====\nimport java.util.ArrayList;\n\npublic class Main {\n    public static void main(String[] args) {\n        ArrayList<Performer> lineup = new ArrayList<>();\n        lineup.add(new Band(\"The Low Tides\", 4));\n        lineup.add(new Dj(\"Kora\"));\n        int minutes = 0;\n        for (Performer performer : lineup) {\n            System.out.println(performer);\n            minutes += performer.slotMinutes();\n        }\n        System.out.println(minutes + \" min in all\");\n    }\n}\n","solution":"// ==== Performer.java ====\npublic abstract class Performer {\n    private String name;\n\n    public Performer(String name) {\n        this.name = name;\n    }\n\n    public String getName() {\n        return this.name;\n    }\n\n    public abstract int slotMinutes();\n\n    @Override\n    public String toString() {\n        return this.name + \", \" + this.slotMinutes() + \" min\";\n    }\n}\n\n// ==== Dj.java ====\npublic class Dj extends Performer {\n    public Dj(String name) {\n        super(name);\n    }\n\n    @Override\n    public int slotMinutes() {\n        return 60;\n    }\n}\n\n// ==== Band.java ====\npublic class Band extends Performer {\n    private int members;\n\n    public Band(String name, int members) {\n        super(name);\n        this.members = members;\n    }\n\n    @Override\n    public int slotMinutes() {\n        return 30 + 5 * this.members;\n    }\n}\n\n// ==== Main.java ====\nimport java.util.ArrayList;\n\npublic class Main {\n    public static void main(String[] args) {\n        ArrayList<Performer> lineup = new ArrayList<>();\n        lineup.add(new Band(\"The Low Tides\", 4));\n        lineup.add(new Dj(\"Kora\"));\n        int minutes = 0;\n        for (Performer performer : lineup) {\n            System.out.println(performer);\n            minutes += performer.slotMinutes();\n        }\n        System.out.println(minutes + \" min in all\");\n    }\n}\n","hints":["Performer's name and `slotMinutes()` are in italics, under «abstract»: the class is abstract, and so is that method, a header that ends in a semicolon.","The triangle from Dj points to Performer: `public class Dj extends Performer`. Dj's box has no fields, so the name belongs to Performer, and Dj's constructor passes it on with `super(name);`."],"tests":[{"name":"The program's output","stdin":"","expect":"The Low Tides, 50 min\nKora, 60 min\n110 min in all","hidden":false},{"name":"Test 2","stdin":"","call":"Performer solo = new Band(\"Mira Solen\", 1);\nPerformer dj = new Dj(\"Vex\");\nSystem.out.println(solo.getName() + \" \" + solo.slotMinutes());\nSystem.out.println(dj.getName() + \" / \" + dj);\n","expect":"Mira Solen 35\nVex / Vex, 60 min","hidden":true}],"require":[{"pattern":"\\babstract\\s+class\\s+Performer\\b","message":"The «abstract» above Performer's name means `public abstract class Performer`."},{"pattern":"\\babstract\\s+int\\s+slotMinutes\\s*\\(\\s*\\)\\s*;","message":"`slotMinutes()` is in italics in Performer's box: it's abstract, `public abstract int slotMinutes();`, with no body."},{"pattern":"\\bclass\\s+Dj\\s+extends\\s+Performer\\b","message":"The triangle from Dj to Performer means `public class Dj extends Performer`."}],"forbid":[{"pattern":"\\bclass\\s+Dj\\b[^}]*\\bprivate\\s+String\\s+name\\b","message":"Dj's box has no fields: the name is Performer's. Pass it on with `super(name);`."}]},{"task":"A garden center delivers what its customers buy, by van or by bike. Write the three classes of this diagram, each in its own file:\n\n\n<figure class=\"uml\"><div class=\"uml-scroll\" tabindex=\"0\" role=\"group\" aria-label=\"Class diagram of Delivery, VanDelivery and BikeDelivery\">\n<svg class=\"uml-svg\" viewBox=\"0 0 600 282\" width=\"600\" height=\"282\" style=\"width:37.5rem;min-width:34.38rem;aspect-ratio:600/282\" font-size=\"12\" aria-hidden=\"true\" focusable=\"false\"><path class=\"u-edge\" fill=\"none\" d=\"M323.5 153L323.5 164L169 164L169 188\"/><path class=\"u-edge\" fill=\"none\" d=\"M323.5 153L323.5 164L478 164L478 188\"/><path class=\"u-tri\" d=\"M323.5 142L316.5 153L330.5 153Z\"/><rect class=\"u-bg\" fill=\"none\" x=\"221.5\" y=\"6\" width=\"204\" height=\"136\"/><rect class=\"u-head\" fill=\"none\" x=\"221.5\" y=\"6\" width=\"204\" height=\"40\"/><text x=\"323.5\" y=\"22\" text-anchor=\"middle\" class=\"u-st\">«abstract»</text><text x=\"323.5\" y=\"38\" text-anchor=\"middle\" class=\"u-name u-it\">Delivery</text><text x=\"229.5\" y=\"62\">-address: String</text><text x=\"229.5\" y=\"86\">+Delivery(address: String)</text><text x=\"229.5\" y=\"102\">+getAddress(): String</text><text x=\"229.5\" y=\"118\" class=\"u-it\">+cost(): int</text><text x=\"229.5\" y=\"134\">+toString(): String</text><path class=\"u-sep\" fill=\"none\" d=\"M221.5 46h204M221.5 70h204\"/><rect class=\"u-frame\" fill=\"none\" x=\"221.5\" y=\"6\" width=\"204\" height=\"136\"/><rect class=\"u-bg\" fill=\"none\" x=\"6\" y=\"188\" width=\"326\" height=\"88\"/><rect class=\"u-head\" fill=\"none\" x=\"6\" y=\"188\" width=\"326\" height=\"24\"/><text x=\"169\" y=\"204\" text-anchor=\"middle\" class=\"u-name\">VanDelivery</text><text x=\"14\" y=\"228\">-pallets: int</text><text x=\"14\" y=\"252\">+VanDelivery(address: String, pallets: int)</text><text x=\"14\" y=\"268\">+cost(): int</text><path class=\"u-sep\" fill=\"none\" d=\"M6 212h326M6 236h326\"/><rect class=\"u-frame\" fill=\"none\" x=\"6\" y=\"188\" width=\"326\" height=\"88\"/><rect class=\"u-bg\" fill=\"none\" x=\"362\" y=\"188\" width=\"232\" height=\"72\"/><rect class=\"u-head\" fill=\"none\" x=\"362\" y=\"188\" width=\"232\" height=\"24\"/><text x=\"478\" y=\"204\" text-anchor=\"middle\" class=\"u-name\">BikeDelivery</text><text x=\"370\" y=\"236\">+BikeDelivery(address: String)</text><text x=\"370\" y=\"252\">+cost(): int</text><path class=\"u-sep\" fill=\"none\" d=\"M362 212h232M362 220h232\"/><rect class=\"u-frame\" fill=\"none\" x=\"362\" y=\"188\" width=\"232\" height=\"72\"/></svg>\n</div><details class=\"uml-text\"><summary>The diagram as text</summary><ul><li><strong>Delivery</strong>, an abstract class<ul><li>private field <code>address: String</code></li><li>public constructor <code>Delivery(address: String)</code></li><li>public method <code>getAddress(): String</code></li><li>public abstract method <code>cost(): int</code></li><li>public method <code>toString(): String</code></li></ul></li><li><strong>VanDelivery</strong>, a class that extends Delivery<ul><li>private field <code>pallets: int</code></li><li>public constructor <code>VanDelivery(address: String, pallets: int)</code></li><li>public method <code>cost(): int</code></li></ul></li><li><strong>BikeDelivery</strong>, a class that extends Delivery<ul><li>public constructor <code>BikeDelivery(address: String)</code></li><li>public method <code>cost(): int</code></li></ul></li></ul></details></figure>\n\n\n- A van delivery costs 25 dollars, plus 15 for each pallet. A bike delivery costs 8.\n- Every delivery's `toString` returns `8 Hill Street: 55 dollars`, with its own address and cost.\n\nThe check runs:\n\n```java\nDelivery van = new VanDelivery(\"8 Hill Street\", 2);\nDelivery bike = new BikeDelivery(\"3 Pond Lane\");\nSystem.out.println(van);\nSystem.out.println(bike);\n```\n\nand expects:\n\n```\n8 Hill Street: 55 dollars\n3 Pond Lane: 8 dollars\n```\n","kind":"code","seed":"// ==== Delivery.java ====\npublic class Delivery {\n\n}\n\n// ==== VanDelivery.java ====\npublic class VanDelivery {\n\n}\n\n// ==== BikeDelivery.java ====\npublic class BikeDelivery {\n\n}\n\n// ==== Main.java ====\npublic class Main {\n    public static void main(String[] args) {\n        Delivery van = new VanDelivery(\"8 Hill Street\", 2);\n        Delivery bike = new BikeDelivery(\"3 Pond Lane\");\n        System.out.println(van);\n        System.out.println(bike);\n    }\n}\n","solution":"// ==== Delivery.java ====\npublic abstract class Delivery {\n    private String address;\n\n    public Delivery(String address) {\n        this.address = address;\n    }\n\n    public String getAddress() {\n        return this.address;\n    }\n\n    public abstract int cost();\n\n    @Override\n    public String toString() {\n        return this.address + \": \" + this.cost() + \" dollars\";\n    }\n}\n\n// ==== VanDelivery.java ====\npublic class VanDelivery extends Delivery {\n    private int pallets;\n\n    public VanDelivery(String address, int pallets) {\n        super(address);\n        this.pallets = pallets;\n    }\n\n    @Override\n    public int cost() {\n        return 25 + 15 * this.pallets;\n    }\n}\n\n// ==== BikeDelivery.java ====\npublic class BikeDelivery extends Delivery {\n    public BikeDelivery(String address) {\n        super(address);\n    }\n\n    @Override\n    public int cost() {\n        return 8;\n    }\n}\n\n// ==== Main.java ====\npublic class Main {\n    public static void main(String[] args) {\n        Delivery van = new VanDelivery(\"8 Hill Street\", 2);\n        Delivery bike = new BikeDelivery(\"3 Pond Lane\");\n        System.out.println(van);\n        System.out.println(bike);\n    }\n}\n","hints":["Start with Delivery: `public abstract class Delivery`, with the address, a constructor, `getAddress`, `public abstract int cost();`, and a `toString` that calls `cost()`.","Each subclass `extends Delivery`, passes the address on with `super(address);`, and writes `cost()`: 25 plus 15 times the pallets for a van, 8 for a bike."],"tests":[{"name":"Test 1","stdin":"","call":"Delivery van = new VanDelivery(\"8 Hill Street\", 2);\nDelivery bike = new BikeDelivery(\"3 Pond Lane\");\nSystem.out.println(van);\nSystem.out.println(bike);\n","expect":"8 Hill Street: 55 dollars\n3 Pond Lane: 8 dollars","hidden":false},{"name":"Test 2","stdin":"","call":"ArrayList<Delivery> today = new ArrayList<>();\ntoday.add(new VanDelivery(\"1 Oak Row\", 0));\ntoday.add(new BikeDelivery(\"7 Fern Way\"));\ntoday.add(new VanDelivery(\"22 Birch Road\", 5));\nint total = 0;\nfor (Delivery delivery : today) {\n    total += delivery.cost();\n}\nSystem.out.println(today.get(2).getAddress() + \" \" + today.get(2).cost() + \" \" + total);\nSystem.out.println(today.get(0));\n","expect":"22 Birch Road 100 133\n1 Oak Row: 25 dollars","hidden":true}],"require":[{"pattern":"\\babstract\\s+class\\s+Delivery\\b","message":"The «abstract» above Delivery's name means `public abstract class Delivery`."},{"pattern":"\\babstract\\s+int\\s+cost\\s*\\(\\s*\\)\\s*;","message":"`cost()` is in italics in Delivery's box: write it as `public abstract int cost();`."},{"pattern":"\\bextends\\s+Delivery\\b","min":2,"message":"Both triangles point to Delivery: VanDelivery and BikeDelivery each `extends Delivery`."}],"forbid":[]}]}