Java – protected / public internal class
Can someone explain to me the difference between protected / public inner classes?
I know that public internal courses should be avoided as much as possible (as explained in this article)
But as I can see, there is no difference in using protection or public modifiers
Look at this example:
public class Foo1 { public Foo1() { } protected class InnerFoo { public InnerFoo() { super(); } } }
…
public class Foo2 extends Foo1 { public Foo2() { Foo1.InnerFoo innerFoo = new Foo1.InnerFoo(); } }
…
public class Bar { public Bar() { Foo1 foo1 = new Foo1(); Foo1.InnerFoo innerFoo1 = foo1.new InnerFoo(); Foo2 foo2 = new Foo2(); Foo2.InnerFoo innerFoo2 = foo2.new InnerFoo(); } }
All of these are compiled and valid, whether I declare innerfoo protected or public
What am I missing? Please point out that my use is protected or public
thank you.
Solution
Protected access modifiers restrict access from classes in the same package and its subclasses
In the example shown, public and protected will have the same effect as in the same package
For more information about accessing modifiers, the Java tutorials controlling access to members of a class page may be of interest