Chapter 4 · Robust, modern Java

Interfaces and polymorphism

You are reading the whole lesson on one page. Back to one step at a time

Step 1 of 21 · Concept

Same question, different answers

A rectangle and a circle are different classes. A program that adds up areas only needs to ask each one the same question: what is your area?

In this lesson you will:

  • write an interface that names such a question,
  • answer it in several classes,
  • loop over different objects as one list.
public class Main {
    public static void main(String[] args) {
        Shape s = new Rectangle(3, 4);
        System.out.printf("rectangle: %.2f%n", s.area());
        s = new Circle(2);
        System.out.printf("circle: %.2f%n", s.area());
    }
}
public class Main {
    public static void main(String[] args) {
        Shape s = new Square(5);
        System.out.printf("square: %.2f%n", s.area());
    }
}
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<Shape> shapes = List.of(new Square(2),
                new Circle(1), new Rectangle(4, 6));
        for (Shape s : shapes) {
            System.out.printf("%.2f%n", s.area());
        }
    }
}
import java.util.List;

public class Main {
    static double totalArea(List<Shape> shapes) {
        // add up the area of every shape
        return 0;
    }

    public static void main(String[] args) {
        List<Shape> shapes = List.of(new Square(2),
                new Square(3), new Rectangle(4, 6));
        System.out.printf("total: %.2f%n", totalArea(shapes));
    }
}
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<Vehicle> vehicles = List.of(new Bike("Ada"),
                new Car("Grace"));
        for (Vehicle v : vehicles) {
            System.out.println(v.describe());
        }
    }
}
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<Shape> shapes = List.of(new Circle(1),
                new Rectangle(2, 3), new Circle(3));
        for (Shape s : shapes) {
            if (s instanceof Circle c) {
                double r = c.radius();
                System.out.println("a circle of radius " + r);
            } else {
                System.out.println("another shape");
            }
        }
    }
}
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<Animal> animals = List.of(new Dog("Rex"),
                new Cat("Tom"), new Dog("Fido"));
        int dogs = 0;
        // for every animal: print its line,
        // and count the dogs
        System.out.println("dogs: " + dogs);
    }
}
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<Animal> animals = List.of(new Dog("Rex"),
                new Cat("Tom"));
        for (Animal a : animals) {
            System.out.println(a.describe());
        }
    }
}
Terminal
Step 1 of 8An interface and two classes

Keyboard shortcuts

The step

ContinueEnter
Previous / next stepAlt+[ · Alt+]
Move along the steps bar← → Home End, then Enter
From a quiz's title to its answers↓
Skip to the stepTab, then Enter (first on the page)
Course outlineAlt+O
These shortcuts?

The workspace

Run the stepCtrl+Enter (⌘↵ on a Mac)
Lesson → editor → consoleF6 (Shift+F6 back)
Output ↔ TerminalCtrl+`
Focus the editorCtrl+. (⌘. on a Mac)
Hide or show the lessonAlt+L
Restore, close a messageEsc (outside the editor and the terminal)

The editor

Leave the editorF6 (Shift+F6 back); or Ctrl+M (⌃⇧M on a Mac), then Tab moves the focus (not in the vim mode)
Open a file by nameCtrl+P (⌘P on a Mac)
All commandsCtrl+Shift+P (⌘⇧P on a Mac) or F1
Go to a lineCtrl+G (⌃G on a Mac)
Show or hide the filesCtrl+B (⌘B on a Mac)
Previous / next file tabCtrl+Shift+[ · Ctrl+Shift+] (⌘⇧[ · ⌘⇧] on a Mac)
The file tabs← → Home End on a tab; Delete closes it
Save the files nowCtrl+S (⌘S on a Mac)
Go to a symbol in the fileCtrl+Shift+O (⌘⇧O on a Mac)
Go to the definitionF12
Format the fileCtrl+Shift+F (⌘⇧F on a Mac), or Shift+Alt+F
Go back / forward after a jumpAlt+← · Alt+→ (⌃- · ⌃⇧- on a Mac)