14

I have code as follows:

class A
  private
  def p_method
    puts "I'm a private method from A"
  end
end

class B < A
  def some_method
    p_method
  end
end

b = B.new
b.p_method    # => Error: Private method can not be called
b.some_method # => I'm a private method from A

b.some_method calls a private method that is defined in class A. How can a private method be accessed in the class where it is inherited? Is this behavior the same in all object oriented programming languages? How does Ruby do encapsulation?

Venkat Ch
  • 1,168
  • 2
  • 17
  • 37
  • [Are there good reasons for 'private' to work the way it does in Ruby?](https://stackoverflow.com/questions/1565269/are-there-good-reasons-for-private-to-work-the-way-it-does-in-ruby) – cremno Jul 04 '15 at 06:28
  • You are actually accessing the private method `p_method` of your base class from inside the sub class's instance scope, just fine. It's obvious why you can't access it from outside. The private scope is preserved, it won't change when you sub class. – limekin Jul 04 '15 at 06:32
  • How is inheritance or encapsulation relevant to the issue of private methods? – sawa Jul 04 '15 at 07:10
  • @cremno - Thanks. I find it helps. – Venkat Ch Jul 04 '15 at 08:22
  • 1
    Note: `B.private_instance_methods.include? :p_method #=> true`. As to whether this behavior is true in all oop languages, I think you are overreaching to ask that as part of your question. – Cary Swoveland Jul 04 '15 at 08:41
  • @sawa Because private methods cannot be called by a derived class in, e.g., C++, and keeping methods from being called is one of the mechanisms used to enforce encapsulation. – Wayne Conrad Jul 04 '15 at 16:16
  • @WayneConrad We are talking about Ruby, and term "private" in Ruby has nothing to do with "private" in C++. Talking about encapsulation and inheritance, "protected" would be more relevant in Ruby, much more than "private" is. – sawa Jul 04 '15 at 17:10
  • @sawa inheritance and encapsulation surely are relevant to the issue of private methods. Without C++/Java-style private methods, everything is in you public API: you cannot reliably change your “private” implementation without breaking your users, who may accidentally overwrite your “private methods”. Javadoc by default documents protected method for a reason; inheritable methods *are* part of the API. – Franklin Yu Jun 20 '17 at 14:15

1 Answers1

22

Here's a brief explanation from this source:

  1. Public methods can be called by anyone---there is no access control. Methods are public by default (except for initialize, which is always private).
  2. Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.
  3. Private methods cannot be called with an explicit receiver. Because you cannot specify an object when using them, private methods can be called only in the defining class and by direct descendants within that same object.

This answer from a similar question expands on the topic in more detail: https://stackoverflow.com/a/1565640/814591

Sourabh Upadhyay
  • 1,034
  • 11
  • 31