Learn Ruby and rails from rails guru
Archive for May, 2010
Quick Start Ruby -5, Inheritance
May 22nd
This is a continuation of the following series of posts
Quick Ruby Tutorials-1
Quick Ruby Tutorials-2
Quick Ruby Tutorials-3
Quick Ruby Tutorials-4, Decision Making
def self.x
puts “I m a class method defined in class A”
end
def y
puts “I m an instance method defined in class A”
end
end
class B
end
b = B.new
B.x # no method error
b.y # no method error
The errors are obvious as we the methods x and y do not exist in class B.
end
b = B.new
B.x # “I m a class method defined in class A”
b.y # “I m an instance method defined in class A”
In the defination of class B, the part “< A" tells ruby that class B is inheriting from class A.
When class B inherits from class A, all the properties(methods, instance and class variables) in class A are available to class B
Syntax is as follows
end
Let us take another example
attr_accessor :sound
def initialize(s)
puts "in initialize of Animal class"
self.sound = s
end
def make_sound
puts self.sound + “ “ + self.sound
end
end
Let us define a class dog which inherits from animal
end
d = Dog.new(“bhow”) #in initialize of Animal class
d.make_sound # “bhow bhow”
Here we need to pass the sound of dog, every time a new dog is born.
But dog will always make the sound “bhow”. We need not pass the value for sound every time we create an instance of the Dog. For this we would need to override the initialize method in the Dog class as shown.
def initialize
puts "in initialize of Dog class"
self.sound = “bhow”
end
end
d = Dog.new #in initialize of Dog class
d.make_sound # “bhow bhow”
Note that in the above Dog class we have overridden the initialize method of the animal class(base class).
This overrides the initialize method of the base class(irrespective of the number of arguments).
So when the dog object is created, the initialize method of the Animal class is not executed.
There will be many occurrences when we only want to slightly modify the behaviour of the base class method, like
- add some more processing to the output of the method.
- call the method with some specific parameters to the method.
For this we need to access the base class(Animal) method in the derived class(Dog) method.
We can do this with the keyword “super”.
def initialize
puts "in initialize of Dog class"
super(“bhow”)
end
end
d = Dog.new
#in initialize of Dog class
#in initialize of Animal class
d.make_sound # bhow bhow
As we see first the initialize method in the Dog is called, which calls the initialize method in the Animal class with parameter “bhow”
Let us consider another use of “super”. We want the dog to bark 4 times when the make sound is called, unlike the make_sound method of the animal class which makes sound 2 times.
def initialize
puts "in initialize of Dog class"
super(“bhow”)
end
def make_sound
super
super
end
end
d = Dog.new
#in initialize of Dog class
#in initialize of Animal class
d.make_sound # bhow bhwo bhow bhow
Above we called super 2 times in the make_sound function. Due to this the sound is printed 4 times.
Let us take another example for overriding.
def to_s
"the string version of this array is: " + super
end
end
x = MyArray.new(['a','b','c'])
x.to_s # the string version of this array is: abc
In the above example we saw how we processed the output of the base class method, in the child class method.
Share

