Should you ever use protected member variables?

Should you ever use protected member variables? Depends on how picky you are about hiding state. If you don’t want any leaking of internal state, then declaring all your member variables private is the way to go. If you don’t really care that subclasses can access internal state, then protected is good enough. If a … Read more

Why can a class not be defined as protected?

Because it makes no sense. Protected class member (method or variable) is just like package-private (default visibility), except that it also can be accessed from subclasses. Since there’s no such concept as ‘subpackage’ or ‘package-inheritance’ in Java, declaring class protected or package-private would be the same thing. You can declare nested and inner classes as … Read more

Why is Java’s AbstractList’s removeRange() method protected?

Yes, because that’s not how you remove a range from outside code. Instead, do this: list.subList(start, end).clear(); This actually calls removeRange behind the scenes.† The OP asks why removeRange is not part of the List public API. The reason is described in Item 40 of Effective Java 2nd ed, and I quote it here: There … Read more

What’s the best way to unit test protected & private methods in Ruby?

You can bypass encapsulation with the send method: myobject.send(:method_name, args) This is a ‘feature’ of Ruby. 🙂 There was internal debate during Ruby 1.9 development which considered having send respect privacy and send! ignore it, but in the end nothing changed in Ruby 1.9. Ignore the comments below discussing send! and breaking things.

What is the difference between private and protected members of C++ classes?

Private members are only accessible within the class defining them. Protected members are accessible in the class that defines them and in classes that inherit from that class. Edit: Both are also accessible by friends of their class, and in the case of protected members, by friends of their derived classes. Edit 2: Use whatever … Read more

What is the difference between public, private, and protected?

You use: public scope to make that property/method available from anywhere, other classes and instances of the object. private scope when you want your property/method to be visible in its own class only. protected scope when you want to make your property/method visible in all classes that extend current class including the parent class. If … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)