10 Replies Latest reply: Jul 24, 2007 3:51 AM by 807600 RSS

    Protected Inner Class problem

    807600
      //:prac/innerclasses/pkg/InsidePackage.java
      package innerclasses.pkg;
      
      public interface InsidePackage {
           void display();
      }
      //:prac/innerclasses/pkg2/InnerClass.java
      package innerclasses.pkg2;
      import innerclasses.pkg.*;
      
      public class InnerClass{
           protected class Inner implements InsidePackage{
                public void display(){
                     System.out.println("Hello");
                }
           }
      }
      //:prac/innerclasses/pkg3/InnerClass2.java
      package innerclasses.pkg3;
      import innerclasses.pkg2.InnerClass;
      import innerclasses.pkg.*;
      
      public class InnerClass2 extends InnerClass{
           public InsidePackage insidePackage(){
                return new Inner();
           }
           
           public static void main(String[] args){
                InnerClass2 ic2 = new InnerClass2();
                InsidePackage ip = ic2.insidePackage();
                ip.display();
           }
      }
      When i compile InnerClass2.java
      i get this message:

      innerclasses\pkg3\InnerClass2.java:8: Inner() has protected access in innerclass
      es.pkg2.InnerClass.Inner
      return new Inner();
      ^
      1 error


      since the class Inner has a protected access, therefore it should be accessible to the classes that is inheriting this class, but still i'm gettin this error...can anyone explain this?
        • 1. Re: Protected Inner Class problem
          807600
          since the class Inner has a protected access, therefore it should be
          accessible to the classes that is inheriting this class, but still i'm
          gettin this error...can anyone explain this?
          the class Inner is accessible, that's not what the compiler is complaining about. it's compaining about the constructor of class Inner
          Inner() has protected access in
          you don't provide an explicit constructor in class Inner; so you get the compiler-generated default one; default constructor has same access as class; so ctor is protected, so only accessible to class Inner and its subclasses and classes in same package
          • 2. Re: Protected Inner Class problem
            807600
            since the class Inner has a protected access,
            therefore it should be
            accessible to the classes that is inheriting this
            class, but still i'm
            gettin this error...can anyone explain this?
            the class Inner is accessible, that's not what the
            compiler is complaining about. it's compaining about
            the constructor of class Inner
            Inner() has protected access in
            you don't provide an explicit constructor in class
            Inner; so you get the compiler-generated default one;
            default constructor has same access as class; so ctor
            is protected, so only accessible to class Inner and
            its subclasses and classes in same package
            so shall i write the constructor explicitly like this:

            public Inner();
            • 3. Re: Protected Inner Class problem
              807600
              so shall i write the constructor explicitly like
              this:

              public Inner();
              that's what i think, yes. your compiler will tell you for sure.

              so?
              • 4. Re: Protected Inner Class problem
                807600
                so shall i write the constructor explicitly like
                this:

                public Inner();
                that's what i think, yes. your compiler will tell you
                for sure.

                so?
                i did that and it started to work, but my ques is: you said that the Inner class constructor too has protected access as Inner class is havin protected access, that means it shod be avaibable to the classes that is inheritin, since class InnerProtect2is extendin InnerProtect, so Inner shod be accessible to InnerProtect2, shodn't it?
                • 5. Re: Protected Inner Class problem
                  807600
                  i did that and it started to work, but my ques is:
                  what the hell is a ques? write like a normal person please.
                  you said that the Inner class constructor too has
                  protected access as Inner class is havin protected
                  access, that means it shod be avaibable to the
                  classes that is inheritin, since class
                  InnerProtect2is extendin InnerProtect, so Inner shod
                  be accessible to InnerProtect2, shodn't it?
                  i can hardly read what you are writing here.
                  re-read what i wrote before. write it down in your own words if you have to.

                  a class and its constructor are not the same. a protected constructor is accessible to subclasses of the declaring class (that's Inner). InnerProtect2 does not extend Inner, nor is it in the same package. so no, "it shodnt".
                  a protected member class is accessible to subclasses of the declaring class. that is, class Inner is accessible to InnerProtect2, but its constructor isn't.
                  • 6. Re: Protected Inner Class problem
                    807600
                    What you may not be aware of is that the default constructor of an inner class is not empty. It has one argument, which is the enclosing type
                    • 7. Re: Protected Inner Class problem
                      807600
                      i did that and it started to work, but my ques is:
                      what the hell is a ques? write like a normal person
                      please.
                      you said that the Inner class constructor too has
                      protected access as Inner class is havin protected
                      access, that means it shod be avaibable to the
                      classes that is inheritin, since class
                      InnerProtect2is extendin InnerProtect, so Inner
                      shod
                      be accessible to InnerProtect2, shodn't it?
                      i can hardly read what you are writing here.
                      re-read what i wrote before. write it down in your
                      own words if you have to.

                      a class and its constructor are not the same. a
                      protected constructor is accessible to subclasses of
                      the declaring class (that's Inner). InnerProtect2
                      does not extend Inner, nor is it in the same package.
                      so no, "it shodnt".
                      a protected member class is accessible to subclasses
                      of the declaring class. that is, class Inner is
                      accessible to InnerProtect2, but its constructor
                      isn't.
                      Hii OnBringer,

                      thanx for ur valuable replies, i read what u've written n tried to implement it, this is wat i tried
                      //:prac/innerclasses/pkg2/InnerClass5.java
                      package innerclasses.pkg2;
                      import innerclasses.pkg.*;
                      
                      class InnerClass5 extends InnerClass{
                           InsidePackage insidePackage(){
                                return new Inner();
                           }
                           
                           public static void main(String[] args){
                                InnerClass5 ic5 = new InnerClass5();
                                InsidePackage ip = ic5.insidePackage();
                                ip.display();
                           }
                      }
                      Inner is accessible n there is no compile time error, as InnerClass5 is in the same package, n that proves ur point, thanx for ur time n replies
                      • 8. Re: Protected Inner Class problem
                        807600
                        u r wlcm
                        pls xprss tx by writing normally instd of ur sms
                        • 9. Re: Protected Inner Class problem
                          807600
                          u r wlcm
                          pls xprss tx by writing normally instd of ur sms
                          aha i will.....thank you :)
                          • 10. Re: Protected Inner Class problem
                            807600
                            you're welcome