Inheritance in Ruby

 Uncategorized  Add comments
Mar 202009
 

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

  One Response to “Inheritance in Ruby”

  1. NO COMMENT~!

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

© 2012 Programming & technology Suffusion theme by Sayontan Sinha