Protected Inner Class problem

//:prac/innerclasses/pkg/InsidePackage.java

package innerclasses.pkg;

publicinterface InsidePackage{

void display();

}

//:prac/innerclasses/pkg2/InnerClass.java

package innerclasses.pkg2;

import innerclasses.pkg.*;

publicclass InnerClass{

protectedclass Innerimplements InsidePackage{

publicvoid display(){

System.out.println("Hello");

}

}

}

//:prac/innerclasses/pkg3/InnerClass2.java

package innerclasses.pkg3;

import innerclasses.pkg2.InnerClass;

import innerclasses.pkg.*;

publicclass InnerClass2extends InnerClass{

public InsidePackage insidePackage(){

returnnew Inner();

}

publicstaticvoid 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?

[2672 byte] By [lavia] at [2007-11-27 11:31:26]
# 1

> 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

OnBringera at 2007-7-29 16:39:42 > top of Java-index,Java Essentials,New To Java...
# 2

> > 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();

lavia at 2007-7-29 16:39:42 > top of Java-index,Java Essentials,New To Java...
# 3

> 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?

OnBringera at 2007-7-29 16:39:42 > top of Java-index,Java Essentials,New To Java...
# 4

> > 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?

lavia at 2007-7-29 16:39:42 > top of Java-index,Java Essentials,New To Java...
# 5

> 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.

OnBringera at 2007-7-29 16:39:42 > top of Java-index,Java Essentials,New To Java...
# 6

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

georgemca at 2007-7-29 16:39:42 > top of Java-index,Java Essentials,New To Java...
# 7

> > 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

lavia at 2007-7-29 16:39:42 > top of Java-index,Java Essentials,New To Java...
# 8

u r wlcm

pls xprss tx by writing normally instd of ur sms

OnBringera at 2007-7-29 16:39:42 > top of Java-index,Java Essentials,New To Java...
# 9

> u r wlcm

> pls xprss tx by writing normally instd of ur sms

aha i will.....thank you :)

lavia at 2007-7-29 16:39:42 > top of Java-index,Java Essentials,New To Java...
# 10

you're welcome

OnBringera at 2007-7-29 16:39:42 > top of Java-index,Java Essentials,New To Java...