Encapsulation is hiding piece of functionality and making it unavailable to the rest of the code base.

Encapsulation is used to restrict direct access to the states of an object.

To access and modify the state of an object, publicly accessible methods (generally getter and setter methods) are provided.

class Customer
  def initialize(id, n, addr)
    @id = id
    @name = n
    @address = addr

  def display_details
    puts "Customer id: #{id}"
    puts "Customer name: #{name}"
    puts "Customer address: #{address}"


  attr_reader :id, :name, :address

josh = Customer.new('0001', 'Josh', '26 ave Grand Marnier')

In the example above, the states of the object josh instantiated from Customer class are private, i.e., cannot be accessed directly. However, the object has one behavior publicly available: instance method display_details.

Advantages of Encapsulation

  • Data Hiding: from outside, the implementation of the class is hidden and code only interacts with setter/getter methods.
  • Reusability: as long as the public interfaces are still public, the implementation is of no concern and therfore, the reusability is improved.
  • Testing: encapsulated codes are easy to test with unit testing.
Links to this page