How to create a private class method?

private doesn’t seem to work if you are defining a method on an explicit object (in your case self). You can use private_class_method to define class methods as private (or like you described). class Person def self.get_name persons_name end def self.persons_name “Sam” end private_class_method :persons_name end puts “Hey, ” + Person.get_name puts “Hey, ” + … Read more

Understanding private methods in Ruby

Here’s the short and the long of it. What private means in Ruby is a method cannot be called with an explicit receivers, e.g. some_instance.private_method(value). So even though the implicit receiver is self, in your example you explicitly use self so the private methods are not accessible. Think of it this way, would you expect … Read more

Why does Ruby have both private and protected methods?

protected methods can be called by any instance of the defining class or its subclasses. private methods can be called only from within the calling object. You cannot access another instance’s private methods directly. Here is a quick practical example: def compare_to(x) self.some_method <=> x.some_method end some_method cannot be private here. It must be protected … Read more

What is a good example to differentiate between fileprivate and private in Swift3

fileprivate is now what private used to be in earlier Swift releases: accessible from the same source file. A declaration marked as private can now only be accessed within the lexical scope it is declared in. So private is more restrictive than fileprivate. As of Swift 4, private declarations inside a type are accessible to … Read more

Private virtual method in C++

Herb Sutter has very nicely explained it here. Guideline #2: Prefer to make virtual functions private. This lets the derived classes override the function to customize the behavior as needed, without further exposing the virtual functions directly by making them callable by derived classes (as would be possible if the functions were just protected). The … Read more

What is the default access specifier in Java?

The default visibility is known as “package-private” (though you can’t use this explicitly), which means the field will be accessible from inside the same package to which the class belongs. As mdma pointed out, it isn’t true for interface members though, for which the default is “public”. See Java’s Access Specifiers