Monday 30 May 2016

Class Inheritance


Continue with previous post

require 'person_class'  
  
class Programmer < Person  
  def initialize(name, favorite_ide)  
    super(name)  
    @favorite_ide = favorite_ide  
  end  
  
  # We are overriding say_hi in Person  
  def say_hi  
    super  
    puts "Favorite IDE is #{@favorite_ide}"  
  end  
end  
  
peter = Programmer.new("Peter", "TextMate")  
peter.say_hi


In the above mentioned class we are creating a class called Programmer and inheriting the Person class which is created in previous post.

In the Programmer class we are overriding the "say_hi" definition which is all ready defined in the Person class.

Practice this code and give me the output result as comment.

<<Back

In next you are going to setter and getter methods in ruby. 

No comments:

Post a Comment

My Experience in RUBYCONF INDIA 2016

Hi Readers, Rubyconf India is a global event complementing other RubyConf events across the world. I attended this event held on 19-20 ma...