Inheritance သည် class တစ်ခုက အခြား class တစ်ခုရဲ့ behavior တွေကို အမွေဆက်ခံတာပါ။ Shared behavior ကို parent class ထဲမှာထားပြီး child class တွေက ထပ်တိုး behavior ထည့်နိုင်ပါတယ်။
ruby
class Vehicle
def start_engine
puts "Engine started!"
end
end
class Car < Vehicle
def drive
puts "Driving..."
end
end
my_car = Car.new
my_car.start_engine
my_car.driveCar < Vehicle ဆိုတာ Car က Vehicle ကို inherit လုပ်တယ်လို့ဆိုလိုပါတယ်။ ဒါကြောင့် Car object က start_engine ကိုသုံးနိုင်ပါတယ်။
You should see
Engine started! Driving...Info
Inheritance ကို “is-a” relationship ဖြစ်တဲ့အခါသုံးပါ။ Car is a Vehicle ဆိုတာသဘာဝကျပါတယ်။