Inheritance in Ruby
Inheritance is one of Object-Oriented concept. Since Ruby is an Object-Oriented Programming, it has implemented the Inheritance functionality.
#intialize global variable
$value = 0
class Parent
def parent_value
$value = 5
end
end
class Child < Parent
def child_value
$value = 10
end
end
child = Child.new
#calling parent_method using child object
base_value = child.parent_value
#calling child_method
derive_value = child.child_value
#display result
puts "Here's parent value: #{base_value}"
puts "Here's child value: #{derive_value}"
Sample Result:
| Here’s parent value: 5 Here’s child value: 10 |
Filed under Ruby & Rails. Tags: Ruby
NO COMMENT~!