0

If an outer class can’t have “protected” modifier in Java because the sub-classes of it will only be able to use it, why Java allows a modifier for classes in a source file(other than public), even though its scope of accessibility is even lower?

class A{
   private class B{}
   protected class C{}
   public class D{}
   class E{}
}
public class Test{
public static void main(String[] args){
   System.out.println("Test class");
   }
}

Here the outer class A of the single source file has default access modifier. Now, bottom is a "A" class in the source file that's not allowed in Java is:

protected class A{
   private class B{}
   protected class C{}
   public class D{}
   class E{}
}
public class Test{
public static void main(String[] args){
   System.out.println("Test class");
   }
}

My question is if a default modifier is allowed for an outer class, why a protected modifier isn't allowed even though default modifier has a lesser scope than . Protected can be accessed through Same class, Same package and Sub-class. Default can be accessed through only Same class and Same package. Why is the second process invalid even though the first one is valid. My question is if protected doesn't make sense how can default make sense?

Sami
  • 31
  • 3
  • 2
    Welcome to Stack Overflow! Please take the [tour], have a look around, and read through the [help], in particular [*How do I ask a good question?*](/help/how-to-ask) I'm afraid it's not at all clear what you're asking here. One thing that would help would be source code showing clearly what structure you're talking about, and then a fuller explanation of what you think that source code defines, and what about that is the focus of your question. – T.J. Crowder Jan 17 '18 at 13:16
  • 1
    A top level class can either have the public visibility modifier or none (which then means package visibility). Anything else isn't supported. Of course that doesn't apply for inner classes. – Thomas Jan 17 '18 at 13:22
  • Possible duplicate of https://stackoverflow.com/questions/3869556/why-a-class-cannot-be-defined-as-protected – Klitos Kyriacou Jan 17 '18 at 13:23
  • I checked the other Q/As, but those doesn't address the question I have which is more of a logical question than of problem solving. I am new here, so please forgive me if I'm doing errors and I will try to be as specific and unique as I could. – Sami Jan 18 '18 at 07:26
  • It's still unclear what you're asking really - especially as you refer to classes "in the same source file" for no obvious reason. (That doesn't make any difference.) Bear in mind that the subclass access granted to protected members refers to subclasses of the containing class - there *is* no containing class for outer classes. – Jon Skeet Jan 18 '18 at 07:35

0 Answers0