aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in a JQ+ question... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in a JQ+ question..." Watch "Doubt in a JQ+ question..." New topic
Author

Doubt in a JQ+ question...

Isabel Wanderley
Ranch Hand

Joined: Aug 24, 2002
Posts: 42
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

Joined: Aug 29, 2002
Posts: 223
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.


Whatever doesn't kill us ...<br />Is probably circling back for another try.<br />SCJP 1.4
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
In the Java Tutorial it explains this behaviour.
Look at the section "Protected" and take notice of the last part of the section.
-Barry


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
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

Joined: Aug 24, 2002
Posts: 42
I got it in Java tutorial... I accepted but I didn't understand why that happens....
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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

Joined: Aug 24, 2002
Posts: 42
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

Joined: Aug 03, 2002
Posts: 7729
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

Joined: Aug 05, 2002
Posts: 787
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

Joined: Aug 03, 2002
Posts: 7729
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

Joined: Aug 26, 2001
Posts: 7610
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.


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in a JQ+ question...