Skip to content

Instantly share code, notes, and snippets.

@sk187

sk187/ruby_inheritance.rb

Last active Aug 29, 2015
Embed
What would you like to do?
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