Implementation
Turn the design into working code: define the base class, implement concrete shapes, and print results from a mixed list.
Implementation is a free Java Academy lesson on CoddyKit — lesson 2 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.
Plan recap
We will code the base Shape with area() and perimeter(), then add Circle and Rectangle. Keep names short and math readable, and prefer small helper methods for printing.
Code: base + Circle
Base class defines the API and a small info() helper. Circle implements the math and toString() for clear output.
public class Main {
// Base class with a tiny print helper
static abstract class Shape {
abstract double area();
abstract double perimeter();
String info() {
return String.format("area=%.2f, per=%.2f", area(), perimeter());
}
}
static class Circle extends Shape {
double radius;
Circle(double r) {
// simple guard to avoid negative sizes for beginners
radius = (r < 0) ? 0 : r;
}
double area() { return Math.PI * radius * radius; }
double perimeter() { return 2 * Math.PI * radius; }
public String toString() { return "Circle " + info(); }
}
public static void main(String[] args) {
Shape s = new Circle(2.5);
System.out.println(s); // prints Circle area/perimeter
}
}
Constructors & guards
Constructors set fields. For beginners, guard against negative inputs by clamping to zero. Real apps use validation or exceptions, but here we keep it simple.
Code: add Rectangle
Rectangle joins the hierarchy and uses the same API. Notice how both shapes can be printed with the same code because the base defines the contract.
public class Main {
static abstract class Shape {
abstract double area();
abstract double perimeter();
String info() { return String.format("area=%.2f, per=%.2f", area(), perimeter()); }
}
static class Circle extends Shape {
double radius;
Circle(double r) { radius = (r < 0) ? 0 : 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 < 0) ? 0 : w;
this.h = (h < 0) ? 0 : 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 a = new Circle(3.0);
Shape b = new Rectangle(4.0, 2.0);
System.out.println(a);
System.out.println(b);
}
}
Lists & printing
A list of Shape lets us mix Circle and Rectangle. A simple loop can print each item. For mobile output, keep one shape per line and format numbers to two decimals.
Code: tiny report
Report: a tiny loop prints type and summary for each shape. Polymorphism keeps the loop clean while each shape provides its own math.
public class Main {
static abstract class Shape {
abstract double area();
abstract double perimeter();
String label() { return getClass().getSimpleName(); }
String info() { return String.format("area=%.2f, per=%.2f", area(), perimeter()); }
}
static class Circle extends Shape {
double radius;
Circle(double r) { radius = (r < 0) ? 0 : r; }
double area() { return Math.PI * radius * radius; }
double perimeter() { return 2 * Math.PI * radius; }
}
static class Rectangle extends Shape {
double w, h;
Rectangle(double w, double h) { this.w = (w < 0) ? 0 : w; this.h = (h < 0) ? 0 : h; }
double area() { return w * h; }
double perimeter() { return 2 * (w + h); }
}
public static void main(String[] args) {
Shape[] shapes = { new Circle(1.2), new Rectangle(2.0, 3.0), new Circle(0.5) };
for (int i = 0; i < shapes.length; i = i + 1) {
Shape s = shapes[i];
System.out.println(s.label() + " -> " + s.info());
}
}
}
Implementation check
Quick check: Which is required for each concrete shape in this hierarchy?
Recap
Recap: We coded Shape, Circle, and Rectangle; added simple guards; and printed a tiny report from a mixed list. Next lesson: polymorphic behaviors like sorting by area.
Frequently asked questions
Is the “Implementation” lesson free?
Yes — the full text of “Implementation” 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 “Implementation”?
Turn the design into working code: define the base class, implement concrete shapes, and print results from a mixed list. 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 2 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “Implementation” 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.
All lessons in this course
- Design
- Implementation
- Polymorphic Behaviors