ruby restrict attr_accessor in subclass
- by Arivarasan
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"