Board index » jbuilder » Confusion with protected inner classes

Confusion with protected inner classes


2004-06-24 07:58:01 AM
jbuilder12
Please see this example
/////////////////////////////////////////////////////////////
package aPackage;
public class Dog {
protected class Bone {
public void eat() {
System.out.println("eating bone...");
}
}
}
/////////////////////////////////////////////////////////////
package otherPackage;
import aPackage.*;
class Doberman extends Dog {
public Bone makeBone() {
return new Bone(); // ERROR!!!!
}
}
/////////////////////////////////////////////////////////////
Jbuilder reports that Bone() cannot be accessed, because it has protected
access, but I understand that inherited classes can access base's protected
members like for example a protected inner class. So what's going on here?
Thanks
 
 

Re:Confusion with protected inner classes

Carlos Zin wrote:
Quote
Please see this example

/////////////////////////////////////////////////////////////
package aPackage;
public class Dog {
protected class Bone {
public void eat() {
System.out.println("eating bone...");
}
}
}
/////////////////////////////////////////////////////////////
package otherPackage;
import aPackage.*;
class Doberman extends Dog {
public Bone makeBone() {
return new Bone(); // ERROR!!!!
}
}
/////////////////////////////////////////////////////////////
Jbuilder reports that Bone() cannot be accessed, because it has protected
access, but I understand that inherited classes can access base's
protected members like for example a protected inner class. So what's
going on here? Thanks
Look at the example below. Runs fine.
public class ClassA {
public ClassA() {
}
protected class Bone {
public void speak() {
System.out.println("Woof!! Woof!!");
}
}
}
public class ClassB extends ClassA {
public ClassB() {
}
public Bone getBone() {
return new Bone();
}
public static void main(String[] args) {
ClassB classB1 = new ClassB();
Bone b=classB1.getBone();
b.speak();
}
}
It sounds as though, you are attempting to call the Doberman inner class
Bone from without the Doberman or parent class. That will not work, sinc<e
it has not been defined outside of the parent or child class.
Within the class or any inhereting class, you should be able to access it.
Example;
public class ClassC {
public ClassC() {
}
public static void main(String[] args) {
ClassC classC1 = new ClassC();
ClassB cb= new ClassB();
Bone b= cb.getBone();
}
}
This will not work, because there is no defined Bone class. However, this
will work:
public class ClassC {
public ClassC() {
}
public static void main(String[] args) {
ClassC classC1 = new ClassC();
ClassB cb= new ClassB();
Bone b= cb.getBone().speak();
}
}
Why? Because you are accessing the inner class and the public method from
within ClassB. Hope this makes it clear..
You can also access the protected inner class Bone, as follows in ClassC
public class ClassC {
public ClassC() {
}
public static void main(String[] args) {
ClassC classC1 = new ClassC();
ClassB cb= new ClassB();
ClassB.Bone b= cb.getBone();
}
}
Hope this helps your understanding..
 

Re:Confusion with protected inner classes

Thanks, but your example runs fine as long as ClassA and ClassB are in the
same package but when put in different packages I get the same error
message: "you can't access Bone(), class Bone has protected access in
package blah blah....etc..."
By the way I found that in the subclass Doberman I can inherit from Bone
with no problem, indicating that in some way Doberman does have access to
the protected inner class Bone but it does not allow me to construct Bone
objects inside the subclass.
CZ
"Paul Nichols (TeamB)" < XXXX@XXXXX.COM >escribi?en el mensaje
Quote
Carlos Zin wrote:

>Please see this example
>
>/////////////////////////////////////////////////////////////
>package aPackage;
>public class Dog {
>protected class Bone {
>public void eat() {
>System.out.println("eating bone...");
>}
>}
>}
>/////////////////////////////////////////////////////////////
>package otherPackage;
>import aPackage.*;
>class Doberman extends Dog {
>public Bone makeBone() {
>return new Bone(); // ERROR!!!!
>}
>}
>/////////////////////////////////////////////////////////////
>Jbuilder reports that Bone() cannot be accessed, because it has
protected
>access, but I understand that inherited classes can access base's
>protected members like for example a protected inner class. So what's
>going on here? Thanks


Look at the example below. Runs fine.

public class ClassA {


public ClassA() {

}
protected class Bone {
public void speak() {
System.out.println("Woof!! Woof!!");
}
}

}

public class ClassB extends ClassA {

public ClassB() {
}

public Bone getBone() {
return new Bone();
}
public static void main(String[] args) {
ClassB classB1 = new ClassB();
Bone b=classB1.getBone();
b.speak();
}

}

It sounds as though, you are attempting to call the Doberman inner class
Bone from without the Doberman or parent class. That will not work, sinc<e
it has not been defined outside of the parent or child class.

Within the class or any inhereting class, you should be able to access it.

Example;

public class ClassC {
public ClassC() {
}
public static void main(String[] args) {
ClassC classC1 = new ClassC();
ClassB cb= new ClassB();
Bone b= cb.getBone();
}

}

This will not work, because there is no defined Bone class. However, this
will work:

public class ClassC {
public ClassC() {
}
public static void main(String[] args) {
ClassC classC1 = new ClassC();
ClassB cb= new ClassB();
Bone b= cb.getBone().speak();
}

}

Why? Because you are accessing the inner class and the public method from
within ClassB. Hope this makes it clear..

You can also access the protected inner class Bone, as follows in ClassC

public class ClassC {
public ClassC() {

}

public static void main(String[] args) {
ClassC classC1 = new ClassC();
ClassB cb= new ClassB();
ClassB.Bone b= cb.getBone();
}

}


Hope this helps your understanding..

 

{smallsort}

Re:Confusion with protected inner classes

For example this one runs fine (the 3 classes are on 3 different packages)
////////////////////////////////////////////////////////////////////////////
//////////
package a;
class Human {
protected class Brain {
protected int size;
public Brain(int s) {
size = s;
}
public int getSize() {
return size;
}
void think() {
System.out.println("thinking...");
}
}
}
////////////////////////////////////////////////////////////////////////////
//
package b;
import a.*;
class Einstein extends Human {
public class SuperBrain
extends Human.Brain {
private int size;
public SuperBrain(Human h, int s) {
h.super(s);
size = super.size * 2;
}
public int getSize() {
return size;
}
void think() {
System.out.println("relatively thinking...");
}
}
}
//////////////////////////////////////////////////////////////////////
import a.*;
import b.*;
public class Example {
public static void main(String[] args) {
Human h = new Human();
Einstein e = new Einstein();
Einstein.SuperBrain es = e.new SuperBrain(h, 10);
es.think();
System.out.println("The size of Einstein's brain is " + es.getSize());
}
}
///////////////////////////////////////////////////////////////////////
"Carlos Zin" < XXXX@XXXXX.COM >escribi?en el mensaje
Quote
Thanks, but your example runs fine as long as ClassA and ClassB are in the
same package but when put in different packages I get the same error
message: "you can't access Bone(), class Bone has protected access in
package blah blah....etc..."

By the way I found that in the subclass Doberman I can inherit from Bone
with no problem, indicating that in some way Doberman does have access to
the protected inner class Bone but it does not allow me to construct Bone
objects inside the subclass.
CZ

"Paul Nichols (TeamB)" < XXXX@XXXXX.COM >escribi?en el mensaje
news: XXXX@XXXXX.COM ...
>Carlos Zin wrote:
>
>>Please see this example
>>
>>/////////////////////////////////////////////////////////////
>>package aPackage;
>>public class Dog {
>>protected class Bone {
>>public void eat() {
>>System.out.println("eating bone...");
>>}
>>}
>>}
>>/////////////////////////////////////////////////////////////
>>package otherPackage;
>>import aPackage.*;
>>class Doberman extends Dog {
>>public Bone makeBone() {
>>return new Bone(); // ERROR!!!!
>>}
>>}
>>/////////////////////////////////////////////////////////////
>>Jbuilder reports that Bone() cannot be accessed, because it has
protected
>>access, but I understand that inherited classes can access base's
>>protected members like for example a protected inner class. So what's
>>going on here? Thanks
>
>
>Look at the example below. Runs fine.
>
>public class ClassA {
>
>
>public ClassA() {
>
>}
>protected class Bone {
>public void speak() {
>System.out.println("Woof!! Woof!!");
>}
>}
>
>}
>
>public class ClassB extends ClassA {
>
>public ClassB() {
>}
>
>public Bone getBone() {
>return new Bone();
>}
>public static void main(String[] args) {
>ClassB classB1 = new ClassB();
>Bone b=classB1.getBone();
>b.speak();
>}
>
>}
>
>It sounds as though, you are attempting to call the Doberman inner class
>Bone from without the Doberman or parent class. That will not work,
sinc<e
>it has not been defined outside of the parent or child class.
>
>Within the class or any inhereting class, you should be able to access
it.
>
>Example;
>
>public class ClassC {
>public ClassC() {
>}
>public static void main(String[] args) {
>ClassC classC1 = new ClassC();
>ClassB cb= new ClassB();
>Bone b= cb.getBone();
>}
>
>}
>
>This will not work, because there is no defined Bone class. However,
this
>will work:
>
>public class ClassC {
>public ClassC() {
>}
>public static void main(String[] args) {
>ClassC classC1 = new ClassC();
>ClassB cb= new ClassB();
>Bone b= cb.getBone().speak();
>}
>
>}
>
>Why? Because you are accessing the inner class and the public method
from
>within ClassB. Hope this makes it clear..
>
>You can also access the protected inner class Bone, as follows in ClassC
>
>public class ClassC {
>public ClassC() {
>
>}
>
>public static void main(String[] args) {
>ClassC classC1 = new ClassC();
>ClassB cb= new ClassB();
>ClassB.Bone b= cb.getBone();
>}
>
>}
>
>
>Hope this helps your understanding..
>


 

Re:Confusion with protected inner classes

On 6/23/2004 at 7:58:01 PM, Carlos Zin wrote:
Quote
/////////////////////////////////////////////////////////////
package aPackage;
public class Dog {
protected class Bone {
public void eat() {
System.out.println("eating bone...");
}
}
}
/////////////////////////////////////////////////////////////
package otherPackage;
import aPackage.*;
class Doberman extends Dog {
public Bone makeBone() {
return new Bone(); // ERROR!!!!
}
}
/////////////////////////////////////////////////////////////
Jbuilder reports that Bone() cannot be accessed, because it has protected
access, but I understand that inherited classes can access base's
protected members like for example a protected inner class. So what's
going on here?
Note that the error is in accessing the constructor Bone(), not the class
Bone. Doberman extends Dog, so Doberman has access to protected members
of Dog, including the class Bone.
However, since the class Bone does not explicitly declare any
constructors, an implicit, no-argument constructor is generated for it.
And since Bone is declared with the "protected" qualifier, the implicit
constructor has protected access as well.
Class Doberman extends Dog, so it has access to Dog's protected members.
However, it does not extend Bone, so it does not have access to Bone's
protected members, including the implicit constructor.
If you add a public constructor to the class Bone, it should work. i.e.
package aPackage;
public class Dog {
protected class Bone {
public Bone() {}
public void eat() {
System.out.println("eating bone...");
}
}
}
--
Regards,
John McGrath [TeamB]
---------------------------------------------------
Before sending me e-mail, please read:
www.JPMcGrath.net/newsgroups/e-mail.html
 

Re:Confusion with protected inner classes

Thanks a lot. This is the solution
Carlos
"John McGrath [TeamB]" < XXXX@XXXXX.COM >escribi?en el mensaje
Quote
On 6/23/2004 at 7:58:01 PM, Carlos Zin wrote:

>/////////////////////////////////////////////////////////////
>package aPackage;
>public class Dog {
>protected class Bone {
>public void eat() {
>System.out.println("eating bone...");
>}
>}
>}
>/////////////////////////////////////////////////////////////
>package otherPackage;
>import aPackage.*;
>class Doberman extends Dog {
>public Bone makeBone() {
>return new Bone(); // ERROR!!!!
>}
>}
>/////////////////////////////////////////////////////////////
>Jbuilder reports that Bone() cannot be accessed, because it has
protected
>access, but I understand that inherited classes can access base's
>protected members like for example a protected inner class. So what's
>going on here?

Note that the error is in accessing the constructor Bone(), not the class
Bone. Doberman extends Dog, so Doberman has access to protected members
of Dog, including the class Bone.

However, since the class Bone does not explicitly declare any
constructors, an implicit, no-argument constructor is generated for it.
And since Bone is declared with the "protected" qualifier, the implicit
constructor has protected access as well.

Class Doberman extends Dog, so it has access to Dog's protected members.
However, it does not extend Bone, so it does not have access to Bone's
protected members, including the implicit constructor.

If you add a public constructor to the class Bone, it should work. i.e.

package aPackage;
public class Dog {
protected class Bone {
public Bone() {}
public void eat() {
System.out.println("eating bone...");
}
}
}

--
Regards,

John McGrath [TeamB]

---------------------------------------------------
Before sending me e-mail, please read:
www.JPMcGrath.net/newsgroups/e-mail.html
 

Re:Confusion with protected inner classes

"Carlos Zin" < XXXX@XXXXX.COM >wrote in message
Quote
Please see this example

/////////////////////////////////////////////////////////////
package aPackage;
public class Dog {
protected class Bone {
public void eat() {
System.out.println("eating bone...");
}
}
}
/////////////////////////////////////////////////////////////
package otherPackage;
import aPackage.*;
class Doberman extends Dog {
public Bone makeBone() {
return new Bone(); // ERROR!!!!
}
}
/////////////////////////////////////////////////////////////
Jbuilder reports that Bone() cannot be accessed, because it has protected
access, but I understand that inherited classes can access base's
protected
members like for example a protected inner class. So what's going on here?
Thanks






Protected means you can never access methods/attributes from within other
packages.
If you extend the class, then you are only able to access the methods and
attributes from the extended class, in your example, new Doberman() would
work.
 

Re:Confusion with protected inner classes

"Jean Charbonneau" < XXXX@XXXXX.COM >wrote in message
Quote

Protected means you can never access methods/attributes from within other
packages.
If you extend the class, then you are only able to access the methods and
attributes from the extended class, in your example, new Doberman() would
work.


My example is bad forget it :)