• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in a JQ+ question...

 
Isabel Wanderley
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I have a doubt in a JQ+ question... this is the question:

It says that won't compile, but I didn't understand why... can anyone explain me how it works?
 
Shishio San
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
The variable i has a protected access in the super class. You can only access it through references of type B or its subclasses.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the Java Tutorial it explains this behaviour.
Look at the section "Protected" and take notice of the last part of the section.
-Barry
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does Java NOT allow access to a protected member in a package within class A from A's subclass B in another package, via an object of type A?
I understand it doew not. But why not?
[ October 06, 2002: Message edited by: Barkat Mardhani ]
 
Isabel Wanderley
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it in Java tutorial... I accepted but I didn't understand why that happens....
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following argument may explain it:
Suppose you are outside the package in which the class C that has the protected method cMethod() or member cMember is defined.
Now suppose C aC = new C();
If you could access aC.cMethod() or aC.cMember from this external package
then the access modifier protected would be equivalent to the public modifier, which defeats the object of having a protected qualifier.
That is a contradiction so the original
hypothesis has to be false.
How does that look?
(It could also be wrong of course)
[ October 07, 2002: Message edited by: Barry Gaunt ]
 
Isabel Wanderley
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Barry
Ok, but the protected modifier doen's says that is accessible to subclasses and classes in the same package??? Because B is a subclasse I thought that he can see the protected method, not just if B is a subclass AND stays in the same package...
So, the protected is for the same package OR subclass in the same package not in different package???
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am also confused by this protected access
in Java.
My own concept of protected would be that a protected member variable or method must only be available
to a subclass method or constructor; with
no access allowed whatsoever via an instance.
-Barry
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I spent some time experimenting with the issue of accessiblity of protected members from inside and outside the package. Here are my findings:
1. Within same package, the protected members are accessble via the objects of their native class (where they are declared) or any sub-classes of their native class. These objects can be created in any class in the same package.
2. From another package, the protected members are accessable only via the objects of sub-classes of their native class. These objecs must be created within the sub-classes. It seems that outside the package, the protected members behave like private members of the subclasses.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Barkat,
that's what is said in the Tutorial/JLS.
But you previously asked "why is it so?".
Have you any ideas on that yet?
-Barry
[ October 08, 2002: Message edited by: Barry Gaunt ]
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One important thing that hasn't been mentioned yet is that a protected member is only accessible by types that are involved in the implementation of the accessed type that owns the protected member.
In the above code, a.i is not accessible within the process() method because i is accessed through the object a (of type A) passed as parameter and not through the inheritance. Within class B, i can be accessed only with the "this" reference but never with a reference to an object of the superclass A.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With all due respect Valentin, I think we are aware
of that. It's in the Tutorial/JLS .
The thing we are struggling with is Barkat's "Why?"
There is no explanation in JLS or the Tute why
this seemingly puzzling semantics of protected was chosen.
-Barry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic