protected/public Inner Classes
Asked Answered
P

3

15

Can someone please explain to me what is the difference between protected / public Inner classes?

I know that public inner classes are to avoid as much as possible (like explained in this article).

But from what I can tell, there is no difference between using protected or public modifiers.

Take a 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 this compiles and is valid whether I declare InnerFoo protected or public.

What am I missing? Please, point me out a case where there's a difference in using protected or public.

Thanks.

Patagium answered 27/2, 2009 at 15:17 Comment(0)
I
23

The protected access modifier will restrict access from classes other than the ones in the same package and its subclasses.

In the example shown, the public and protected will have the same effect, as they are in the same package.

For more information on access modifiers, the Controlling Access to Members of a Class page of The Java Tutorials may be of interest.

Inefficacious answered 27/2, 2009 at 15:20 Comment(4)
Ok. I see my confusion. Because inner classes are like members of the enclosing class I was thinking in terms of protected members. Thanks.Patagium
@bruno conde: protected classes are like protected members.Davidson
In other words, "protected" in Java, whether applied to members or inner classes, is different from C++ and C#. It grants access to other classes in the same package (namespace).Ivy
@Ivy : bit late to the party... but protected in Java does allow access to other classes in the same Package, to the declaring class and all subclasses (wheter in the same package or not). However as far as I know there are no package hierarchy where access modifiers are concerned. That is classes in packages prefixed by the package name of the declaring class will not have access.Tranquilize
O
1

You can just think protected inner class is protected member, so it only access for class, package, subclass but not for the world.

In addition, for outter class, there is only two access modifier for it. Just public and package.

Ottavia answered 22/6, 2014 at 8:39 Comment(0)
B
1

Weird thing in java:

Pure Java: You cannot return a private inner class from a public getter.

In JSP : You cannot return a non-public inner class from a public getter.


Java Demo You Can Run:

public class ReturnInnerClass{
    public static void main(String []args){
        MyClass inst = new MyClass("[PROP_VAL]");
        System.out.println(

            inst.get().myProperty()

        );;    
    };;
};;

class MyClass{ 
    //:If JSP: MUST be public
    //:Pure Java: 
    //:     public,protected,no-access-modifier
    //:     Will all work.
    //:Private fails in both pure java & jsp.
    protected class Getters{
        public String 
        myProperty(){ return(my_property); }
    };;

    //:JSP EL can only access functions:
    private Getters _get;
    public  Getters  get(){ return _get; }

    private String 
    my_property;

    public MyClass(String my_property){
        super();
        this.my_property    = my_property;
        _get = new Getters();
    };;
};;

//:How to run this example:
//:  1: Put this code in file called: "ReturnInnerClass.java"
//:  2: Put ReturnInnerClass.java into it's own folder.
//:     ( Folder name does not matter.)
//:  3: Open the folder.
//:  4: Right-Click --> GitBashHere
//:  5: In command prompt within folder:
//:     5.1: javac ReturnInnerClass.java
//:     5.2: java  ReturnInnerClass
//:     ( javac: java compiler              )
//:     ( java : runs compiled java program )

//:  EXPECTED OUTPUT:
//:  [PROP_VAL]

For JSP, put only the class code above into folder: com/myPackage/MyClass and put "import com.myPackage.MyClass" as first line of source code. Then create a new .jsp page with this source code:

<%@ taglib uri   ="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page import="com.myPackage.MyClass" %>
<%
    MyClass inst = new MyClass("[PROP_VALUE]");
    pageContext.setAttribute("my_inst", inst ); 
%><html lang="en"><body>
    ${ my_inst.get().myProperty() }
</body></html>

Stack Used: Java8 + Tomcat9

Benghazi answered 16/7, 2018 at 5:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.