We already have three classes: Person, Dog, and Cat. Let's add some properties and methods to cat and dog. Both cat and dog will have following properties:
size
property, and the Cat will have
cuteness_rate
property.
Both classes should be able to make some sound.
However, the sound they both do should be different.
Ok, let's start:
class Person
# person class code here
# ...
end
class Dog
attr_accessor :legs, :color, :size
def initialize(opts={})
@legs = 4
@color = opts[:color] || "Black"
@size = opts[:size] || "Medium"
end
def make_sound
p "Bark!"
end
end
class Cat
attr_accessor :legs, :color, :cuteness_rate
def initialize(opts={})
@legs = 4
@color = opts[:color] || "Red"
@cuteness_rate = opts[:cuteness_rate] || 100
end
def make_sound
p "Meow!"
end
end
# Let's comment everything about Person for now:
# john = Person.new
# p john.learned_languages
# john.learn "Ruby"
# john.learn "Python"
# john.learn "JavaScript"
# p john.learned_languages
cat = Cat.new color: "White"
dog = Dog.new size: "Big"
p "Cat's color: #{cat.color}"
cat.make_sound
p "Dog's color: #{dog.color}"
dog.make_sound
We should be able to see something like this:
Let's come back to Inheritance now. Inheritance is a kind of relation between classes where one class is based on another one. You can use class X as a base for a class Y. For example, we could use our class Person as a base class for new classes Student and Teacher because both would share same basic properties and methods. If something in a child class (the one that inherits from a base class) is different, it can always be overridden.
I think you already understand that we can simplify our Dog and Cat classes since both are animals. Let's create a third class called Animal. It would have following properties:
make_sound
method as well:
class Animal
attr_accessor :legs, :color, :size
def initialize(opts={})
@legs = 4
@color = opts[:color] || "Black"
@size = opts[:size] || "Medium"
end
def make_sound
p "hmmm..."
end
end
This code looks very similar to our Dog and Cat classes. We can now
reuse class Animal - Cat and Dog would "derive" (or will be a subclass)
from Animal, and
they will be its exact copies for now. To make Dog and Cat
inherit Animal class, we just need to do next:
class Animal
# animal class code here ...
end
class Dog < Animal
end
class Cat < Animal
end
As you can see, to make some class inherit from another, we just need
to add
<
symbol, and specify parent class.
Right now both Dog and Cat classes are just copies of an Animal class -
Dog won't bark, and Cat won't meow. Cat also lost its
cuteness_level
parameter.
If we try to execute the code, it will work, but
not as we would expect: instead of
meow
or
bark
,
animals will say "hmmm...". However app will not crash - even without
specified parameters and initialization code for Cat and Dog,
Ruby will know that both classes have
make_sound
method and that both have color or size parameters:
So, how to make them work? Very easy, we just add whatever we want, and change whatever is wrong:
class Animal
attr_accessor :legs, :color, :size
def initialize(opts={})
@legs = 4
@color = opts[:color] || "Black"
@size = opts[:size] || "Medium"
end
def make_sound
p "hmmm..."
end
end
class Dog < Animal
def make_sound
p "Bark!"
end
end
class Cat < Animal
attr_accessor :cuteness_level
def make_sound
p "Meow!"
end
end
We have learned almost all essential Ruby features. To start working on Rubymotion apps we just need to learn one last thing - Modules.