ruby restrict attr_accessor in subclass

Posted by Arivarasan on Stack Overflow See other posts from Stack Overflow or by Arivarasan
Published on 2012-11-03T09:29:10Z Indexed on 2012/11/03 11:01 UTC
Read the original article Hit count: 300

Filed under:
|
|

I want restrict the access of superclass's method in subclass

class Parent
  attr_accessor :first_name, :last_name

  def initialize(first_name, last_name)
    @first_name, @last_name = first_name, last_name
  end

  def full_name
  @first_name + " " + @last_name 
  end

end

class Son < Parent
  attr_accessor :first_name

  def initialize(parent, first_name)
    @first_name = first_name 
    @last_name = parent.last_name
  end

  def full_name
    @first_name + "  " + @last_name 
  end
end


p = Parent.new("Bharat", "Chipli")
puts p.full_name

s = Son.new(p, "Harry")
s.last_name= "Smith"
puts s.full_name

here i am getting son's full name as "Harry Smith", but i want "Harry Chipli"

© Stack Overflow or respective owner

Related posts about ruby

Related posts about inheritance