Created
July 17, 2013 07:32
-
-
Save rtoal/6018426 to your computer and use it in GitHub Desktop.
Demonstration of Inheritance and Polymorphism in Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Animal | |
def initialize(name) | |
@name = name | |
end | |
def speak() | |
"#{@name} says #{sound()}" | |
end | |
end | |
class Cow < Animal | |
def sound() | |
"moo" | |
end | |
end | |
class Horse < Animal | |
def sound() | |
"neigh" | |
end | |
end | |
class Sheep < Animal | |
def sound() | |
"baaaaa" | |
end | |
end | |
if __FILE__ == $0 | |
s = Horse.new "CJ" | |
puts(s.speak()) | |
c = Cow.new("Bessie") | |
puts(c.speak()) | |
puts(Sheep.new("Little Lamb").speak()) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment