Protected

`protected` is an access specifier in object-oriented languages

Public

`public` is an access-specifier in object-oriented languages; it indicates that all objects have access to the `public` field or method.

Package accessible accessibility

Quality Example
More restrictive

"Otherwise there is still the possibility to change a public access to a protected or package private access which will be much more restrictive than public"

from question "Is there a way to do unit tests using private variables without increasing their visibility?"

Less accessible

"Since package protected is less accessible than public the code is reducing the accessibility of the foo method"

from question "Why must the methods of a private inner interface be public?"

"Change it to public to make it more accessible or perhaps better protected to make it less accessible than public but more than private read more about that here"

from question "'inaccessible due to its proctecion level'?"

Constructor someone class

Quality Example
Less

"Oh i see you need a constructor that has more access than protected but less than public"

from question "Doing an API, constructor for system private use only and another official, javadoc'ed and public to use. Advise on fix / pattern approach"

Access more

"The theory is that someone extending your class with protected access knows more about what they are doing than someone who is merely using it with public access"

from question "I have a question about OOP .I Need Your help !"

Much more

"Therefore the only types that should have access to a constructor are its derived types and hence protected makes much more sense than public"

from question "Why should constructors on abstract classes be protected, not public?"

Closer perfect method

Quality Example
Actually closer

"Furthermore protected is actually closer to public than to private"

from question "Java : Accessor methods vs protected fields"

Better

"It s not perfect but i guess that a protected method that does not check parameters is better than a public method that does not do it"

from question "Public (static) swap() method vs. redundant (non-static) private ones"

"That means private is better than protected protected is better than public etc .."

from question "Methods in Java"

More restrictive

"So a protected is more restrictive than public and private is more restrictive than protected"

from question "Method visibility when extending an abstract class in PHP"

"Protected is more restrictive than public"

from question "Why is Java's "protected" less protected than default?"

Closer

"Protected is closer to public than private"

from question "What's better practice? Protected / getter?"

Others

Quality Example
Weaker invariant

"Protected functions maintain a weaker invariant than the public one before and after each call"

from question "When/why to make function private in class?"

Better

"Reason i once heard that protected is better for hibernate performance but all i can find on the web is hibernate can access public private and protected accessor methods as well as public private and protected fields directly"

from question "Create the perfect JPA entity"

More permissive

"For public inheritance it means that all the accessible members of base class that is the public and protected members can not be at a level more permissive than public"

from question "Do Sub-Classes Really Inherit Private Member Variables?"

Easier

"What is the true rationale behind all the private and protected stuff when we can just make our life as a programmer easier by using public for everything"

from question "Why make class members private?"

More limited

"From a software engineering point of view as soon as you make a function protected you expose it to other users of course much more limited than public exposure but still much more exposed than private and you create expectation about some stability of the api"

from question "C++ accessing protected member function of Base class with an object of Derived class"

Higher

"Public is higher than protected so there s no problem"

from question "Is it good practice override methods with a higher visibility?"

Back to Home
Data comes from Stack Exchange with CC-BY-SA-3.0