Final challenge: a small family of classes
After this: Predict, complete and explain programs built from a parent class and its children.
This challenge has five steps, and each step is scored on its own. You will predict what a family of classes prints, write a child class's first line, say which method runs for each object in a list, write the line that calls the parent's constructor, and choose between "is a" and "has a". Every step shows the code in Python and in Java, and the answers are the same in both. You can try as many times as you like, and hints work as they do in the lessons.
Step 1 of 5Predict it
Predict it
Circle replaces name; Rect does not. What does this print?
class Shape: def name(self): return "shape"
class Circle(Shape): def name(self): return "circle"
class Rect(Shape): def sides(self): return 4
for s in [Circle(), Rect(), Shape()]: print(s.name())import java.util.List;
class Shape { String name() { return "shape"; }}
class Circle extends Shape { @Override String name() { return "circle"; }}
class Rect extends Shape { int sides() { return 4; }}
public class Main { public static void main(String[] args) { for (Shape s : List.of(new Circle(), new Rect(), new Shape())) { System.out.println(s.name()); } }}Write the class line
Complete the first line of Rect so that it is built on Shape. The program should print shape and then 4.
class Shape: def name(self): return "shape"
____________________ # ← Rect, built on Shape def sides(self): return 4
r = Rect()print(r.name())print(r.sides())class Shape { String name() { return "shape"; }}
____________________ // ← Rect, built on Shape int sides() { return 4; }}
public class Main { public static void main(String[] args) { Rect r = new Rect(); System.out.println(r.name()); System.out.println(r.sides()); }}Which one runs
The list holds a Circle and then a plain Shape. Both go through the same loop.The list and the loop box are both of type Shape. Which area runs on each turn?
class Shape: def area(self): return 0.0
class Circle(Shape): def area(self): return 3.14
shapes = [Circle(), Shape()]for s in shapes: print(s.area())import java.util.ArrayList;import java.util.List;
class Shape { double area() { return 0.0; }}
class Circle extends Shape { @Override double area() { return 3.14; }}
public class Main { public static void main(String[] args) { ArrayList<Shape> shapes = new ArrayList<>(List.of(new Circle(), new Shape())); for (Shape s : shapes) { System.out.println(s.area()); } }}Write the super line
Circle's constructor must let Shape's constructor set name to "circle". Complete the line so that the program prints circle.
class Shape: def __init__(self, name): self.name = name
class Circle(Shape): def __init__(self, r): ____________________ # ← let Shape's constructor set the name to "circle" self.r = r
c = Circle(1)print(c.name)class Shape { String name;
Shape(String name) { this.name = name; }}
class Circle extends Shape { int r;
Circle(int r) { ____________________ // ← let Shape's constructor set the name to "circle" this.r = r; }}
public class Main { public static void main(String[] args) { Circle c = new Circle(1); System.out.println(c.name); }}Is a, or has a
You are writing a Playlist class and already have a Song class. A playlist holds songs and can play them in order. Which link is right?
- Predict what a family of classes prints, write a child class's first line, say which method runs for each object, write the super line, and choose between inheritance and composition.