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.



Fields base members

Example

"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

"I would like to present you with some arguments protecting protected fields in java you may favor accessing base class members using protected fields over public accessors in situation where you need to avoid value validation . however if this is not the case then private fields with public accessors should be used to compliment hermetization"

from question  

Java protected fields vs public getters

"For example it is a compile-time error for a public class to derive from a private or internal class;here you re trying to derive a protected class from a private class - the protected class is more accessible than the private class hence the error"

from question  

C# inherit private class and non-access of public member

"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?

Weaker access property

Example

"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?

"You can just make the property public and not worry about it;you can define public read access with protected write access"

from question  

Angular sub class template accessing a variable defined in a super class

"Therefore public is almost always the way to go;protected doesn t really make sense for daos since you need the methods in other packages in classes which don t implement the dao"

from question  

Spring DAO class method - protected vs public?

"Public as expected means that everyone is given access to either attributes or methods;protected is somewhat less trivial"

from question  

C++ access specifiers

"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?

"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

"Methods in interfaces implicitly have the access modifier of public;so when you implement it with protected it is a weaker access modifier"

from question  

Overriding method that has no access modifier with protected access modifier

"One could stick to a single property if that was made public so implementing implicitly but in that case the property would be exposed much more which is apparently not desired;a protected property cannot implement an interface property implicitly or explicitly"

from question  

Explicit interface implementaion with second implementation

"If you overload the create_transparent_image method you must also define the create_transparent_image method as protected or define the create_transparent_image method as public which is weaker"

from question  

Error on uploading an image

"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 !

"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

Livedata control helper

Example

"It will be complicated for google to support mutablelivedata since the setvalue and postvalue methods are public;where as for livedata they are protected which gives more control"

from question  

Convert LiveData to MutableLiveData

"Addendum correction for controllers you should mark the helper methods as protected private and only the actions private and only the actions should be public"

from question  

Protected and private methods in Rails

Restrictive package default

Example

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

from question  

Java : Accessor methods vs protected fields

"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'?

"In c++ you can consider that public inherited implies a subtyping relationship or you could be more restrictive and consider as subtyping only the cases where the overrides of virtual functions ensure respecting the lsp;and considering that private or protected inherited doesn t but still is a case of subclassing is sane whatever constraints you put or not on virtual function overrides"

from question  

What are the differences between a subtype and subclass?

"Since i don t feel super comfortable with protected private inheritance in c++ i googled it up and came up with this stackoverflow answer difference between private public and protected inheritance"

from question  

Private inheritance in C++ with minimum example.

"Protected is more restrictive than public"

from question  

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

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

from question  

Methods in Java

"Protected is meaningless or reduces to private without inheritance although not in java but with inheritance not in java explodes into a slimy public degenerate and more so in java programming scala chapter 5"

from question  

Why are many developers opposed to using the "protected" modifier in OOP?

"Also using private and protected instead of public is more about good programming design than security and i recommend you read about it as well as polymorphism and inheritance"

from question  

Types in php, retype them and use their variables

"Using default no protected private public is even worse as the exteneding class allows classes in the same package or subclasses to access the logger"

from question  

Best way to define Logger instance in the class

"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

"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?

"Protected is closer to public than private"

from question  

What's better practice? Protected / getter?

" subclass may reside outside the package following scope levels again private default protected public - we can not narrow down the scope;protected is broader scope than default so java does not contradicts so java own guidelines"

from question  

Why does the "protected" modifier in Java allow access to other classes in same package?

"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?

Others

Example

Second though the public quickly becomes more complicated you could use the above but with a protected set and force users to go through a method-based system to modify values allowing you to restrict log and otherwise do some logic during these actions

from question  

Invoking methods with "fingerprints"

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?

Public is higher than protected so there s no problem

from question  

Is it good practice override methods with a higher visibility?

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

I tested out a few variations of this with the following results standalone class own source file class public ctor public success class public ctor protected success class public ctor pkg-protected illegal access class pkg-protected ctor public illegal access static inner class class public ctor public success class protected ctor public success class pkg-protected ctor public illegal access class public ctor protected success class public ctor pkg-protected illegal access non-static inner class class public ctor public illegal access the key points from this are public and protected works but package-protected or less does not work both the class and the ctor must have equal or higher visibility than protected

from question  

How to deserialize a JSON string to a non-public class using JSON-B?

Rotected internal is actually the second most permissive access modifier after public;protected internal s worth noting that protected is arguably more permissive than internal since protected internal allows access from code that you have no control over other assemblies

from question  

What is the difference between "private" and "protected Internal"?

Ince met2 from father has protected you can either override met2 from father with protected or with public;since you do not specify any privilege access met2 from father defaults to package protected which is a level weaker than protected

from question  

About visibility to grandchild class

Making a listener for these three events would be a superior approach if you wanted to make access to these three events public because a superior approach provides a much higher degree of decoupling between the class and a superior approach listeners;making the access protected implies a higher degree of affinity between the class and the listener the designers of the class would end up making a protected runnerlistener interface for the listener adding a protected defaultrunnerlistener implementation for situations when you need to override one or two methods and adding a protected method for you to supply the listener

from question  

Java - Listeners/Observers VS Subclass Protected Methods

I recommend adding protected setters to the parent class or make them public if you have external classes setting these values

from question  

Using childclass attributes with parentclass method

Monotouch sdk follows the .net naming guidelines and public propertie starts with capital letters;private or protected variables starts with lower case

from question  

Binding core plot library in monotouch error

So to quote scott meyers protected is no more encapsulated than public.

from question  

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

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