Arrows for "is a" and "can do"
After this: Read and draw inheritance and an interface: the hollow triangle at the parent, solid or dashed.
On this page · 4 sections · 3 checks
In "Objects that share behaviour" you built Circle and Rect on a class Shape, so that one loop could ask any shape for its area. Lesson 1 drew one class as a box. With two or more boxes, the diagram also has to show how they are linked, and that link is what this lesson draws.
A line between two boxes
A link between two classes is drawn as a line between their boxes. It is called a . There are six kinds, and the line tells you which one: solid or dashed, and what mark sits at each end. This lesson shows the two kinds you already know from code. Lesson 3 shows the other four.
| Kind | Say it as | Drawn as | In code |
|---|---|---|---|
| inheritance | "is a" | solid line, hollow triangle at the parent | class Circle(Shape):class Circle extends Shape |
| interface | "can do" | dashed line, hollow triangle at the interface | the class has the methods (no word for it)class Circle implements Drawable |
Press Run it below. The diagram adds one class and one line at a time, and lights up each new line.
Press Run it to build the diagram one step at a time.
- classes
- 0
- relations
- 0
What the diagram says, step by step:
Shapehas its name in italics. Italics mean the class is : nobody makes a plain shape, only a kind of one.- A solid line runs from
CircletoShape, with a hollow triangle atShape. Read it from the end without the mark: a circle is a shape. Recthas the same kind of line toShape. The triangles always point at the parent, so the lines from the children meet at the top.Drawableis an : a list of methods with no code. The box says «interface» above the name. A dashed line with a hollow triangle runs fromCircletoDrawable: a circle can do whatDrawablelists, which isdraw.
Notice what each child box lists. Circle lists area again because it writes its own version, and draw because it adds it. A child box shows only what the child adds or changes; what it receives unchanged from the parent stays in the parent's box.
The same diagram in code
Here is code that matches the drawing. Every box is a class, and every line is a word in a class's first line.
class Shape: def area(self): return 0.0
class Circle(Shape): def __init__(self, r): self.r = r
def area(self): return 3.14 * self.r * self.r
def draw(self): print("circle, radius", self.r)
class Rect(Shape): def __init__(self, w, h): self.w = w self.h = h
def area(self): return self.w * self.h
print(Rect(3.0, 4.0).area())Circle(1.5).draw()12.0 circle, radius 1.5
interface Drawable { void draw();}
abstract class Shape { abstract double area();}
class Circle extends Shape implements Drawable { private double r;
Circle(double r) { this.r = r; }
double area() { return 3.14 * r * r; }
public void draw() { System.out.println("circle, radius " + r); }}
class Rect extends Shape { private double w; private double h;
Rect(double w, double h) { this.w = w; this.h = h; }
double area() { return w * h; }}
public class Main { public static void main(String[] args) { System.out.println(new Rect(3.0, 4.0).area()); new Circle(1.5).draw(); }}12.0 circle, radius 1.5
How the drawing and the code line up:
- The first line of
Circle, class Circle(Shape):class Circle extends Shape implements Drawable, holds its arrows. Shape in the brackets is the solid line. The dashed line has no word in Python: Circle has a draw method, and that keeps the promise.extends Shape is the solid line, and implements Drawable is the dashed one. - The fields in the boxes,
r,wandh, are the values the constructors store. areaappears in all three boxes, and in all three classes.drawappears only inCircle, the one class with a line toDrawable.
The program makes a rectangle of 3 by 4 and prints its area, 12.0, and then asks a circle of radius 1.5 to draw itself.
Check yourself
Rect is built on Shape: a rectangle is a shape. A solid line joins the two boxes. Where does the hollow triangle go?
Circle can do what the interface Drawable lists. How is the line between them drawn?
A design says: a Dog is an Animal, and a dog can do what the interface Pet lists. In Java the class line is class Dog extends Animal implements Pet {. Which lines leave the Dog box?
Where the arrows point
- Draw is a as a solid line with a hollow triangle at the parent, and read it from the child: Circle is a Shape.
- Draw can do as a dashed line with a hollow triangle at the interface, and read an interface box by its «interface» label.
- Read an abstract class by its name in italics.
- Find each arrow in code: the parent in the first line of the child class (in brackets in Python, after extends in Java), and implements in Java.