Ruby Inheritance
# Inheritance is an important idea for Object Orientated Programming OOP | |
# This example will demo a simple example of inheritance | |
class Dog | |
def talk | |
puts "Woof" | |
end | |
end | |
class JackRussell < Dog | |
end | |
class BichonFrise | |
end | |
roo = JackRussell.new | |
roo.talk | |
#Returns | |
"Woof" | |
candy = BichonFrise.new | |
candy.talk | |
#Returns | |
" undefined method `talk' " | |
# Notice that the class JackRussell inherits from the class Dog on line 10. | |
# While JackRussell has no talk method defined, it inherits the talk method | |
# from the parent or Super class of Dog. This is why roo.talk returns "Woof" | |
# Meanwhile candy.talk returns undefined method as it is talk is not defined | |
# in the class BichonFrise nor did it inherit from the class Dog (line 21) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment