3.5 The World Subclass

The Greenfoot World Subclass and super()

Unit 3: Methods, Worlds, and Constructors · Lesson 3.5 · about 25 minutes · no coding experience needed

You have spent two units inside actors. The world has been quietly sitting there the whole time, and it turns out to be a class you can write code in too. That is where scores, spawning, and the whole setup of a level belong.

What you will be able to do

  • Read a world class declaration and its super call
  • Explain what width, height and cell size mean
  • Predict how cell size changes what a coordinate means on screen

Words you will need

Cell
In plain words The square that coordinates are counted in.
More precisely The grid unit of a Greenfoot world; its pixel size is set at construction.
super
In plain words Set up the built-in World part of me, with these settings.
More precisely A call to the superclass constructor, which must be the first statement.

Build it step by step

Every step below leaves a scenario that compiles and runs. If you stop halfway you will have something that works, not something broken. Follow along in Greenfoot rather than reading straight through.

  1. Step 1

    On your screen A world class declaration open in the editor, showing the class extending World.

    public class OceanWorld extends World
    {
        public OceanWorld()
        {
            super(20, 15, 30);
        }
    }

    This is the whole of a minimal world class, and you met the first line back in 1.1.

    `extends World` puts it in the world family. The method with the same name as the class is its constructor, which is the next lesson. For now, look at super.

  2. Step 2

    On your screen A diagram of a world grid with width, height and cell size labeled.

    super(20, 15, 30);

    Three numbers, in this order, always:

    `20` is the WIDTH in cells. Valid x values are 0 to 19. `15` is the HEIGHT in cells. Valid y values are 0 to 14. `30` is the CELL SIZE in pixels.

    So this world is 20 by 15 cells, drawn at 600 by 450 pixels on screen.

    Note the same off-by-one rule as everywhere else: a width of 20 means the LAST valid x is 19. That will matter enormously in Unit 5.

  3. Step 3

    On your screen Two worlds side by side, one with cell size 1 and one with cell size 30, showing the same coordinate landing in different places.

    super(600, 400, 1);   // 600x400 cells, each 1 pixel
    super(20, 15, 30);    // 20x15 cells, each 30 pixels

    Both of these are 600 by 450-ish pixels on screen, and they behave completely differently.

    With cell size 1, move(4) travels 4 pixels: smooth, fine-grained movement. Most action games use this.

    With cell size 30, move(4) travels 4 CELLS, which is 120 pixels: a big jump. Grid games, board games and tile maps use this, and Unit 6 will use it heavily.

    If your actor "moves too far", the cell size is the first thing to check.

Mistakes almost everyone makes here

These are the wrong ideas students actually build at this point. Read them even if you think you understand, because a wrong idea you have not noticed is the expensive kind.

Common wrong idea The three numbers in super are width, height and speed.

What is actually true Width in cells, height in cells, cell size in pixels.

Why it matters Guessing the third one produces a world the wrong size on screen with no error.

Common wrong idea A cell is always a pixel.

What is actually true A cell is a pixel only when the cell size is 1.

Why it matters This is the root of "my actor moves too far" and of tile map confusion in Unit 6.

Common wrong idea A width of 20 means x can be 20.

What is actually true x runs 0 to 19.

Why it matters The same off-by-one that governs arrays, and worth meeting twice before Unit 5.

Common wrong idea The world is where actor behavior goes.

What is actually true Actor behavior goes in the actor. The world sets the scene and tracks what is shared, like the score.

Why it matters Putting everything in the world produces one enormous class and no reusable actors.

Check your understanding

Answer these before moving on. They are graded instantly and you can retry.

  1. 1. In `super(20, 15, 30)`, what is 30?

  2. 2. A world is created with `super(10, 8, 40)`. What is the largest valid x?

  3. 3. With a cell size of 25, how far across the screen does `move(3)` travel?

  4. 4. Which world would suit a smooth-moving action game?

Fill in the code

Declare a world class for a grid game: 12 cells across, 9 cells down, drawn at 50 pixels per cell.

public class GridWorld extends 
{
    public GridWorld()
    {
        super(12, , );
    }
}
Stuck? Open a hint for each blank
  • Blank 1: Which family does a stage belong to?
  • Blank 2: The second number is how many cells down.
  • Blank 3: The third number is how many pixels one cell takes up.

Lesson quiz

This one counts toward your progress. Take it when the section above makes sense.

  1. 1. The three arguments to super in a Greenfoot world are:

  2. 2. A world is `super(30, 20, 10)`. How big is it on screen?

  3. 3. An actor seems to leap across the screen with move(2). What is worth checking?

  4. 4. What belongs in the world class rather than in an actor?

The short version

  • A world class extends World and calls super in its constructor.
  • super(width, height, cellSize), in cells, cells and pixels.
  • A width of 20 means x runs 0 to 19.
  • move(n) travels n CELLS, so cell size decides how far that is on screen.

If you get stuck

These pages cover the problems that come up most often in this lesson. Opening one is not cheating and it is not counted against you.

Get in Touch

Whether you're a student, parent, or teacher — I'd love to hear from you.

Just want free AP CS resources?

Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]