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

1 Comments.

Leave a comment

Leave a Reply

( Ctrl + Enter )