Menu
Coddy logo textTech

Recap - Shape Hierarchy

Part of the Object Oriented Programming section of Coddy's Lua journey — lesson 36 of 70.

challenge icon

Challenge

Easy

Let's build a complete shape hierarchy from scratch! You'll create a Shape parent class that all shapes inherit from, then build Circle and Square child classes that each have their own unique properties while sharing common functionality.

You'll organize your code across four files:

  • Shape.lua: Your base class that all shapes will inherit from. It should have a :new(color) constructor that stores the shape's color, and a :getColor() method that returns it. This shared property means every shape—whether circle or square—will have a color.
  • Circle.lua: A child class that inherits from Shape. Its :new(color, radius) constructor should call the parent constructor for the color, then add the radius as a unique attribute. Include a :describe() method that prints the circle's details.
  • Square.lua: Another child class inheriting from Shape. Its :new(color, side) constructor should also call the parent constructor, then store the side length. Include a :describe() method for squares as well.
  • main.lua: Bring everything together by creating instances of both shapes and demonstrating that they correctly inherit from Shape while maintaining their unique properties.

You will receive two inputs:

  1. Circle data: color and radius separated by a comma (e.g., blue,5)
  2. Square data: color and side length separated by a comma (e.g., red,4)

In your main file, create a Circle and a Square with the given values. Then print the following on separate lines:

  1. Call :describe() on the circle
  2. Call :describe() on the square
  3. Print the circle's color using :getColor()
  4. Print the square's color using :getColor()

The :describe() method for Circle should print:

Circle with radius {radius}

The :describe() method for Square should print:

Square with side {side}

For example, if the inputs are blue,5 and red,4, the output should be:

Circle with radius 5
Square with side 4
blue
red

This demonstrates the complete inheritance pattern: both shapes inherit :getColor() from Shape (they never define it themselves), while each has its own unique attribute and :describe() method!

Try it yourself

-- main.lua: Bring everything together

local Circle = require('Circle')
local Square = require('Square')

-- Read input for circle (color,radius)
local circleInput = io.read()
local circleColor, circleRadius = circleInput:match("([^,]+),([^,]+)")
circleRadius = tonumber(circleRadius)

-- Read input for square (color,side)
local squareInput = io.read()
local squareColor, squareSide = squareInput:match("([^,]+),([^,]+)")
squareSide = tonumber(squareSide)

-- TODO: Create a Circle instance with the given color and radius

-- TODO: Create a Square instance with the given color and side

-- TODO: Call :describe() on the circle

-- TODO: Call :describe() on the square

-- TODO: Print the circle's color using :getColor()

-- TODO: Print the square's color using :getColor()

All lessons in Object Oriented Programming