Design
Plan the hierarchy: choose the abstract base, list concrete shapes, pick fields and methods, and define simple polymorphic operations.
Design is a free Java Academy lesson on CoddyKit — lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Goal & scope
Goal: Build a small shape hierarchy with an abstract base (Shape) and a couple of concrete shapes (Circle, Rectangle). We will design common operations first, then confirm the plan with tiny code prototypes.
Shapes & fields
Shapes: Circle (radius), Rectangle (width, height). Keep fields only where they belong: radius in Circle, width/height in Rectangle. The base class holds behavior contracts, not every field.
Operations & polymorphism
Common operations: area() and perimeter(). We declare them in Shape so any Shape can answer these calls. Then a list of Shape can hold mixed Circle and Rectangle objects and process them uniformly.
Code: API sketch
Prototype 1: Define Shape with area and perimeter, and a simple Circle. This checks that the shared API works for one concrete type.
public class Main {
// Minimal API sketch to validate the design
static abstract class Shape {
abstract double area();
abstract double perimeter();
}
static class Circle extends Shape {
double radius;
Circle(double r) { radius = r; }
double area() { return Math.PI * radius * radius; }
double perimeter() { return 2 * Math.PI * radius; }
}
public static void main(String[] args) {
Shape s = new Circle(2.0);
System.out.println("area=" + s.area());
System.out.println("perimeter=" + s.perimeter());
}
}
Collections & reporting
We will later store shapes in a list and print summaries like type, area, and perimeter. A small formatter keeps output readable on mobile: one shape per line, rounded to two decimals.
Code: list of shapes
Prototype 2: Mix Circle and Rectangle in one array of Shape and print each. The base methods enable uniform handling (polymorphism).
public class Main {
static abstract class Shape {
abstract double area();
abstract double perimeter();
String info() {
// default helper for neat printing
return String.format("area=%.2f, per=%.2f", area(), perimeter());
}
}
static class Circle extends Shape {
double radius;
Circle(double r) { radius = r; }
double area() { return Math.PI * radius * radius; }
double perimeter() { return 2 * Math.PI * radius; }
public String toString() { return "Circle " + info(); }
}
static class Rectangle extends Shape {
double w, h;
Rectangle(double w, double h) { this.w = w; this.h = h; }
double area() { return w * h; }
double perimeter() { return 2 * (w + h); }
public String toString() { return "Rectangle " + info(); }
}
public static void main(String[] args) {
Shape[] arr = { new Circle(2.0), new Rectangle(3.0, 4.0) };
for (int i = 0; i < arr.length; i = i + 1) {
System.out.println(arr[i]);
}
}
}
Base design check
Quick check: Which items belong in the abstract Shape base class for this project?
Recap
Recap: Base class declares area and perimeter. Subclasses store shape-specific data and implement the math. Lists of Shape let us print mixed shapes with one loop.
Frequently asked questions
Is the “Design” lesson free?
Yes — the full text of “Design” is free to read here on the web, and the Java Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.
What will I learn in “Design”?
Plan the hierarchy: choose the abstract base, list concrete shapes, pick fields and methods, and define simple polymorphic operations. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Java Academy?
No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “Design” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Java Academy lesson?
Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.