Final OOP Check
Part of the Object Oriented Programming section of Coddy's Lua journey — lesson 70 of 70.
Challenge
EasyLet's bring together everything you've learned about OOP in Lua! You'll build a vehicle system that demonstrates inheritance, encapsulation through naming conventions, and mixins for optional capabilities.
You'll organize your code across five files:
Vehicle.lua: The base class for all vehicles. Each vehicle has anameand a private-convention_speed(initialized to 0). Include a:getName()getter, a:getSpeed()getter, and a:setSpeed(value)setter that rejects negative values (if negative, printSpeed cannot be negativeand don't change the value). Add a:describe()method that returns"{name} moving at {speed}".Refuelable.lua: A mixin table containing fuel-related behavior. Include a:refuel(amount)method that adds toself._fuel, and a:getFuel()method that returns the current fuel level.Car.lua: Inherits from Vehicle and uses the Refuelable mixin. The constructor accepts anameand initializes_fuelto 0. Override:describe()to return"{name} moving at {speed}, fuel: {fuel}". Apply the Refuelable mixin to give cars fuel capabilities.Bicycle.lua: Inherits from Vehicle only—no fuel functionality needed. The constructor accepts just aname. Bicycles use the inherited:describe()method without modification.main.lua: Bring everything together! Create a car and a bicycle based on the inputs, demonstrate their different capabilities, and show how they share common vehicle behavior while having distinct features.
You will receive four inputs:
- Car name
- Bicycle name
- Speed to set for both vehicles (a number)
- Fuel amount to add to the car (a number)
In your main file:
- Create a car and a bicycle with the given names
- Set the speed for both vehicles using the third input
- Refuel the car with the fourth input
- Print the car's description
- Print the bicycle's description
For example, if the inputs are Sedan, Mountain Bike, 60, and 45, the output should be:
Sedan moving at 60, fuel: 45
Mountain Bike moving at 60If the inputs are Sports Car, Road Bike, 120, and 80, the output should be:
Sports Car moving at 120, fuel: 80
Road Bike moving at 120This challenge showcases how inheritance provides shared behavior (both vehicles can move), mixins add optional capabilities (only cars need fuel), and encapsulation protects data integrity (speed validation). Each technique serves a different purpose in your design!
Try it yourself
-- main.lua: Bring everything together
local Car = require('Car')
local Bicycle = require('Bicycle')
-- Read inputs
local carName = io.read()
local bicycleName = io.read()
local speed = tonumber(io.read())
local fuelAmount = tonumber(io.read())
-- TODO: Create a car with the given name
-- TODO: Create a bicycle with the given name
-- TODO: Set the speed for both vehicles
-- TODO: Refuel the car with the fuel amount
-- TODO: Print the car's description
-- TODO: Print the bicycle's description
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