In the program below, the child class method calls the parent class' private and protected methods.
class Parent
private
def private_method
'Private Method'
end
protected
def protected_method
'Protected Method'
end
end
class Child < Parent
def get_parent_name
puts private_name
puts protected_method
puts Parent.new.protected_method
puts Parent.new.private_method #Throws an error
end
end
obj = Child.new
obj.get_parent_name
The line
puts Parent.new.private_method #Throws an error
throws an error. The difference I got is that a private method cannot be accessed using explicit receiver, but a protected method can. If this is the only difference between the private and protected methods, and both can be called in sub classes, then what is the use of these methods compared to other languages like java?