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:
Dog
Inherits fromAnimal
, making it a Subclass.Dog
gains both thesay_hello
andeat
methods fromAnimal
.Dog
overrides thesay_hello
method with different functionality.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents