Question

What is the example situation where protected is preferred, but not public? Can someone can elaborate it with example? I have already seen the chat for public, private and protected (see here).

Was it helpful?

Solution

A primary use of protected is cases where a subclass overrides a method that provides some internal details that you don't want to expose to the outside classes. Consider a class like

class BaseAlgorithm {
     run() {
        commonStepOne();
        subclassSpecificStepTwo();
     }

     protected abstract subclassSpecificStepTwo();
}

No one should be calling subclassSpecificStepTwo, so it wouldn't make sense to have it public.

OTHER TIPS

Simply put, protected means that the method (for example) will be accessible only within the subclasses and the package in which it's defined.

Modifier    | Class | Package | Subclass | World
————————————+———————+—————————+——————————+———————
protected   |  ✔    |    ✔    |    ✔     |   ✘
————————————+———————+—————————+——————————+———————

More info:

if a method is protected then it can be accessed within the same package or from the subclass of different package

Have a look at the oracle docs

Question is about usage and use case, so, I have an example.

Consider an abstract class, that will demand it subclass to implement some method, that is not a part of it's public API, and used only inside this class or it's subclasses.

We can't use private modifier, we don't want to make this method public.

So, protected keyword make our API clear, and prevent incapsulation break.

Do you need code example?

All these private, public, protected or no-access modifiers(default) are just a concept to develop to do coding in well organize manner.

You can find some good information from here. The answer you are looking is there.

But there is no such protection, hidden information using any of these modifiers in Java

Consider following code

public class ProtectedPrivacy{

 private String getInfo1(){
 return "private"; 
 }

 protected String getInfo2(){
 return "protected"; 
 }

 String getInfo3(){
 return "default"; 
 }

 public String getInfo4(){
 return "public"; 
 }
}  

Any access level you can violate as follows

public class BreakModifires{

public static void main(String[] args) throws Exception {
   ProtectedPrivacy protectedPrivacy = new ProtectedPrivacy();
   Method method = protectedPrivacy.getClass().getDeclaredMethod("getInfo*", null);
   method.setAccessible(true);
   Object result = method.invoke(protectedPrivacy);
   System.out.println(result.toString());
  }
}       

A public class member makes a promise to the entire universe on behalf of all possible future derivatives and sub-derivatives of a class that all derivatives of that class will support a particular functionality a certain way.

A protected protected member makes a promise on behalf of itself only, to any classes that derive from it, that it will support a particular functionality a certain way; derivatives of that class are free to decide whether or not they wish to take advantage of that functionality, or provide any public methods which would use it.

The reason that public promises are binding on subclasses and protected ones are not is that if a subclasses were not bound by their parents' public promises, there would be no way for code that had a reference to an Animal could safely call MakeNoise on it without knowing whether it was a type which wasn't required to have a MakeNoise member. By contrast, if a class has a protected member, the only way that member can be called is via super. If Animal implemented a protected DetectSmells member and Cat supported it but Fish did not, the fact that Fish doesn't support that method wouldn't affect a SiameseCat's ability to call super.DetectSmells(), since it knows super is a Cat and not a Fish.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top