Recap - Shape Hierarchy
Part of the Object Oriented Programming section of Coddy's Lua journey — lesson 36 of 70.
Challenge
EasyLet'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:
- Circle data: color and radius separated by a comma (e.g.,
blue,5) - 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:
- Call
:describe()on the circle - Call
:describe()on the square - Print the circle's color using
:getColor() - 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
redThis 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
1The 'Self' Concept
Tables with FunctionsExplicit 'self'The Colon SyntaxDot vs ColonRecap - Moving Point4Project: Digital Bank
Project SetupDeposit Method7Polymorphism & Overriding
Overriding MethodsCalling Parent MethodsDuck TypingCommon InterfaceChecking TypeRecap - Employee Roles10Project: Shape Manager
Project SetupRectangle Class2Class Prototype Pattern
The Prototype ConceptLinking with __indexThe :new() ConstructorInitializing AttributesIndependent InstancesRecap - Car Factory5Operator Overloading in OOP
Adding ObjectsSubtracting ObjectsConcatenating ObjectsComparing Objects (<, >)Recap - Wallet Math8Encapsulation
Naming ConventionsClosures for PrivacyAccess via ClosuresRead-Only TablesValidation LogicRecap - Secure Vault11Design Patterns (Lite)
Factory FunctionsSingleton TableIterator PatternObserver (Listener)Recap - Logger Factory3Object State and Behavior
Instance VariablesGetter MethodsSetter MethodsCalculated PropertiesFormatting StringsEquality ChecksRecap - Student Grade6Inheritance Basics
The Inheritance SetupInheriting MethodsExtending the ConstructorAdding Child MethodsShared vs UniqueRecap - Shape Hierarchy