Mar 202009
 

class Encapsulation
    def get_value
        @value
    end

    def set_value value
        @value = value
        return nil
    end
end

encapsulation = Encapsulation.new
encapsulation.set_value(15)
puts encapsulation.get_value

In this code, you cannot mess around with the state of its variable @value. The only way you can access it is through get & set method (get_value, set_value). This is the encapsulation

Sample Result

15

 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