Project Setup
Part of the Object Oriented Programming section of Coddy's Lua journey — lesson 55 of 70.
Challenge
EasyLet's build the foundation for a Shape Manager system! This project will grow over the next several lessons, so we'll start by setting up a clean file structure with a base Shape class.
You'll create two files to organize your code:
Shape.lua: Define a baseShapeclass using the prototype pattern. Every shape has anameattribute (like "Circle" or "Rectangle"). Include a:new(name)constructor that accepts the shape's name, and a:getName()method that returns it.main.lua: Bring your Shape class to life by requiring the module and creating a shape instance. Read a shape name from input, create a Shape with that name, and print the result of calling:getName().
This base class will serve as the parent for specific shapes like Rectangle and Circle in upcoming lessons. For now, focus on getting the class structure right—the __index setup, the constructor pattern, and the getter method.
Input: A single line containing the shape name (a string).
Output: The shape's name returned by :getName().
Try it yourself
-- Require the Shape module
local Shape = require('Shape')
-- Read the shape name from input
local shapeName = io.read()
-- TODO: Create a new Shape instance with the given name
-- TODO: Print the result of calling :getName() on your shape
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