I am somewhat unsure if this is the right place to post this question as it is not related to an issue I have, so please inform me if I should move it.
I have a question about inheritance when writing object-oriented code. Is it a bad practice to create a class that inherits another class that itself inherits a third? As in, create a child class of another child class.
Here is an example to further clarify (only for an example, not code I am planning to use):
-- First class, "grandparent"
Vehicle = {}
Vehicle.__index = Vehicle
function Vehicle.new()
local vehicle = {}
setmetatable(vehicle, Vehicle)
vehicle.velocity = 0
return vehicle
end
function Vehicle:boost()
self.velocity = self.velocity + 5
end
-- Second class and child of first class
Car = {}
Car.__index = Car
setmetatable(Car, Vehicle)
function Car.new()
local car = Vehicle.new()
setmetatable(car, Car)
return car
end
function Car:boost()
print('The car is now boosting')
Vehicle['boost'](self)
end
-- Third class and child of second class
Truck = {}
Truck.__index = Truck
setmetatable(Truck, Car)
function Truck.new()
local truck = Car.new()
setmetatable(truck, Truck)
return truck
end
function Truck:boost()
print('The truck is now boosting')
Car['boost'](self)
end