Access Controls

suggest change

Comparison of access controls in Java and Ruby: If a method is declared private in Java, it can only be accessed by other methods within the same class. If a method is declared protected, it can be accessed by other classes in the same package and by subclasses in a different package. When a method is public, it is visible to everyone. In Java, visibility depends on where the classes lie in the inheritance or package hierarchy.

In Ruby, that hierarchy is not the rule. What matters is which object is the receiver of the method.

For a private method in Ruby, it can never be called with an explicit receiver, except a private setter (self.name = 1), which needs self so the call is not parsed as a local-variable assignment. Otherwise we can only call a private method with an implicit receiver.

This also means we can call a private method from within a class it is declared in as well as all subclasses of this class.

class Test1
  def main_method
    method_private
  end

  private
  def method_private
    puts "Inside methodPrivate for #{self.class}"
  end
end

class Test2 < Test1
  def main_method
    method_private
  end
end

Test1.new.main_method
Test2.new.main_method

# Inside methodPrivate for Test1
# Inside methodPrivate for Test2

class Test3 < Test1
  def main_method
    self.method_private #We were trying to call a private method with an explicit receiver and if called in the same class with self would fail.
  end
end

Test3.new.main_method
# This raises NoMethodError.

# You cannot call the private method from outside the class hierarchy where it was defined.

A protected method can be called with an implicit receiver, like a private method. It can also be called with an explicit receiver when that receiver is self or another instance of the defining class or one of its subclasses.

class Test1
  def main_method
    method_protected
  end

  protected
  def method_protected
    puts "Inside method_protected for #{self.class}"
  end
end

class Test2 < Test1
  def main_method
    method_protected # called by implicit receiver
  end
end

class Test3 < Test1
  def main_method
    self.method_protected # explicit receiver, and the receiver is self
  end
end


# Inside method_protected for Test1
# Inside method_protected for Test2
# Inside method_protected for Test3
class Test4 < Test1
  def main_method
    Test2.new.method_protected # receiver is another subclass of Test1, where the method is protected
  end
end

Test4.new.main_method

class Test5
  def main_method
    Test2.new.method_protected
  end
end

Test5.new.main_method
# This fails because Test5 is not a subclass of Test1.

Consider Public methods with maximum visibility

Summary

  1. Public: Public methods have maximum visibility
  2. Protected: A protected method can be called with an implicit receiver, like a private method. It can also be called with an explicit receiver when that receiver is self or another instance of the defining class or one of its subclasses.
  3. Private: A private method cannot be called with an explicit receiver (except a private setter written as self.name =). Call it with an implicit receiver. That call is allowed in the class where it is declared and in subclasses of that class.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents