Subclasses
suggest changeInheritance allows classes to define specific behaviour based on an existing class.
class Animal
def say_hello
'Meep!'
end
def eat
'Yumm!'
end
end
class Dog < Animal
def say_hello
'Woof!'
end
end
spot = Dog.new
spot.say_hello # 'Woof!'
spot.eat # 'Yumm!'
In this example:
DogInherits fromAnimal, making it a Subclass.Doggains both thesay_helloandeatmethods fromAnimal.Dogoverrides thesay_hellomethod with different functionality.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents