Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes confusion of protected 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 "confusion of protected" Watch "confusion of protected" New topic
Author

confusion of protected

Rishi Chopra
Ranch Hand

Joined: Mar 07, 2004
Posts: 108
Hi all ,

I am preparing from Kathy sierra and bert bates book for SCJP exam ex-310-035
I came across this thing from book page 80 (chapter 2)
------------------------------------------------------------------------
Once the subclass-outside the package inherits the protected member the member becomes private to anycode outside the subclass .So if a class �neighbour� instantiates a child object,then even if class neighbour is in same package as class child,class neighbour would not b able to access to the child protected (inherited)member
------------------------------------------------------------------------
After reading this I created following classes:---

it compiled successfully
and

subclass of it in another package as this:--


both files I compiled and as expected the subclass �subtestProtected� inherits the member and prints value of protected member.
----------------------
output:-
my value==10
-------------------------------------------
But now I create another subclass of above subclass like this:



when I run this class as well it prints value of protected member of subclass that shud have become private accordin to book and would not have been able to print its value..

plss explain this as I am confused
I am using j2sdk1.4_03 version of java standard development kit..

Rgds
Rishi Chopra
[ August 31, 2005: Message edited by: Michael Ernest ]

Life is Beautiful Learn to See it.........
Ryan Kade
Ranch Hand

Joined: Aug 16, 2005
Posts: 69
My copy of the book actually includes this key phrase after your opening sentence: "with the exception of subclasses of the subclass."

Protected members function as private member variables for all subclasses, where they are directly inherited are not.
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

It's a little early for me, but your third class is just another subclass of the first one. It's an indirect subclass, but it's still a subclass.


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Rishi Chopra
Ranch Hand

Joined: Mar 07, 2004
Posts: 108
hi,
yeah i know its indirectly a subclass of first one but
is this indirect sort of subclassing there in java ..

i ams till confused so plz help.

Rgds
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Hi,

The significance of the statement can be gauged if u can modify your code a

wee bit....remove extends of the third class...now this class is not extending any class...and see ...there is a compiler error.

i have pasted the code for you..



Regards
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Now the protected varibale a will be not be accessible even though the object is that of the subclass

Wonder how to change the value of this variable in a real world situation??

Tx
Kalyana Sundaram
Ranch Hand

Joined: Mar 18, 2005
Posts: 94
i had a doubt that the answer may be irelevant. If you remove the extends class then it would be a separate independent entity with no such thing called inheritance.

Originally posted by A Kumar:
Hi,

The significance of the statement can be gauged if u can modify your code a

wee bit....remove extends of the third class...now this class is not extending any class...and see ...there is a compiler error.

i have pasted the code for you..



Regards


In that situtation, it will always produce a compile time error as it does not have any memeber called "a". Please correct me if am wrong.


Only those who will risk going too far can possibly find out how far one can go !!!
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Once the subclass-outside the package inherits the protected member the member becomes private to anycode outside the subclass .So if a class “neighbour” instantiates a child object,then even if class neighbour is in same package as class child,class neighbour would not b able to access to the child protected (inherited)member with the exception of subclasses of the subclass



I believe that the example holds good for the above statement.

Regards
Rishi Chopra
Ranch Hand

Joined: Mar 07, 2004
Posts: 108
hey,

but in kathy sieraa they mentioned that when subclass outside the package access the protected memeber of superclass it becomes private inside that subclass ..

now when another class(subchildtestProtected in my case) tries to access this protected variable that is private inside subclass outside tha package(subtestProtected in my case) i cannot ..

correct me if i am wrong..

anyways plz tel me the right way through it ...sorry disturbing u all again ..but i found this tricky and so am asking to correct my doubt..

Rgds
Rishi Chopra
thatha beginner
Greenhorn

Joined: Aug 29, 2005
Posts: 2
hi,
I have read from a book that ,u can have access to a protected variable only from its immediate subclass.That is you cannot inherit that from muliple subclasses.You can try using the val from the first subclass into the second one.It may help
Kathy Sierra
Cowgirl and Author
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
Hi there,
This is a confusing one for everyone, and we didn't do the best job of making it clear. Here's a little more on this, and an example:

1) The protected variable in a superclass has "default" access to any other class (subclass or not) within the same package as the class declaring the protected member.

2) To any class outside the package (except for a subclass), the protected member is "default", and therefore cannot be seen/used/accessed by any class outside the package that is not a subclass.

3) If the subclass of the class with the protected variable is outside the package of the superclass, THEN the variable is visible to the subclass but ONLY through inheritance. In other words, the subclass "has" the protected member. But... inside the subclass-outside-the-package, that inherited variable does NOT have "default" access to anyone else... except for subclasses of the subclass that inherited the variable.

4) This does NOT mean that "all other subclasses of the original superclass can access one another's copy of the protected instance variable! Subclass B and subclass C, both outside the package of the original class A, for example, cannot access each other's copy of the inherited variable.

5) BUT... imagine that subclass B extends the original class A, and subclass B is in a different package from the original A superclass (A, remember, is the class that actually *declares* the protected member). Now if a third class, subclass C, extends subclass B, then subclass C ALSO inherits the original protected member. So... each subclass can "pass on the inheritance" of the protected member to its own subclasses, but there is NO sharing/access relationship between *different* subclasses of the original superclass.

Here's a little example of part of this:



So... no other class from ANY other package can access the "a" protected member, except for subclasses of the original superclass that declared the protected member. However, any subclass of the original class will inherit the protected member through its superclass. But to any other class outside the original package, the variable behaves as though it as private. The only exception to that rule is that a subclass can still inherit it from its superclass, regardless of the packages.

Hope that helps a little...
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Hi Kathy,

Can you tell me which point did i violate with respect to the piece of code

i added above...

Tx
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Hi,

Now why am I getting compilation error for this...




Tx
lexander Bosco
Ranch Hand

Joined: Feb 21, 2005
Posts: 65
kathy thanks for the refreshment on that.


there is no knowledge that is not power<br />-<br />SCJP 1.4<br />SCWCD in progress<br />SCMAD in progress
srikanth reddy
Ranch Hand

Joined: Jul 28, 2005
Posts: 252
hi kathey ...

class testsuperclass{
protected static int va=5;
}
class testsubclass extends testsuperclass {
}
class testsubclass1 extends testsubclass {
public static void main(String[] args)
{
testsubclass t=new testsubclass();
System.out.println(t.va);
}
}

in this we are able to access the copy of a variable of one subclass in other subclass...

i think iam missing somewhere in ur above explanation ..plz correct me ..

thanks & regards

srikanth reddy


Thanks & Regards<br /> <br />-Srikanth
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Hi srikkanth,

The classess which you have coded is


within the package but if you see my earlier example...its in different package..

Within package the var is accessible with a default specifier..

(see the first point of kathy's explanation)...

so extending..doesnt cause a error.

Regards
Roy Simon
Ranch Hand

Joined: Aug 26, 2005
Posts: 62
Hi Kumar

you are getting the compile error cos u are trying to access the protected member of the superclass via a subclass object and not inheritance

superclass's protected members are visible to subclasses outside its package only by inheritance and not via an object of the subclass...
that's why you were getting the compile error

To be more descriptive

------------------
Within the same package
class superClass
{
protected int i=0;
}
class subClass extends superClass
{
void printI()
{
System.out.println(i);//no issues via inhertance
}
public static void main(String[] a)
{
subClass obj = new subClass();
System.out.println(obj.i);//ok in the same package
}
}
----------
in different packages
package simon1;
public class superClass
{
protected int i = 0;
}
-----
-----
package simon2;
import simon1.*;
public class subClass extends superClass
{
void printI()
{
System.out.println(i);//ok through inheritance
}
public static void main(String[] a)
{
subClass obj = new subClass();
//System.out.println(obj.i);//no way : compile error//i protected in //superClass
obj.printI();//no problems
}
}

hope protected is clear
Regards
Simon Roy
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Thanks Simon
Rishi Chopra
Ranch Hand

Joined: Mar 07, 2004
Posts: 108
hi simon;

i think ur wrong i created the same files u said as follows:--
------------------------------------------------------------
package simon1;

public class superClass {
protected int i = 0;
}
---

package simon2;
import simon1.*;

public class subClass extends superClass{
void printI(){
System.out.println(i);
}

public static void main(String[] args) {
//new superClass().i;
subClass obj = new subClass();
System.out.println(obj.i);
obj.printI();
}
}

-------------

this always happens there will never b compiler error there .... but when u try to access the protected member there in the subclass through parent object u get compiler error for that....

-----------------------
kathy thanks for explanation but tel me this if u can:--

i am still not able to understand whether "what u mean by subclass outside the package has private access to protected member..and ur points 4 and 5 will b true always i guess two subclasses of the same superclass cannot access each others protected inherited member"...fine

Rgds
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

Please make the extra effort to write out words such as "regards", "you", "your", "be", etc. The extra keystrokes won't cost much in the way of time, and the enhanced clarity will be appreciated by those communicating on a forum with international readership.

http://faq.javaranch.com/view?UseRealWords
Roy Simon
Ranch Hand

Joined: Aug 26, 2005
Posts: 62
hi Rishi

package simon1;

public class superClass {
protected int i = 0;
}
---

package simon2;
import simon1.*;

public class subClass extends superClass{
void printI(){
System.out.println(i);
}

public static void main(String[] args) {
//this will complie fine
new superClass().i;
subClass obj = new subClass();
//this too works
System.out.println(obj.i);
obj.printI();
}
}

the problem with my code was that i was using a subclass obj inside the subclass itself ie running the subclass's main function
and recall that all members are accessible from inside a class(private protected and public),that's the reason the complie error did not occur
but it something like the below is done

import simon.*;
import simon2.*;
class Test
{
public static void main(String[] a)
{
subClass obj =new subClass();
//System.out.println(new superClass().i);//compile error :i protected in //superClass
//System.out.println(new subClass().i);//COmpile error: i protected in //superClass
System.out.println(obj.printI());//ok prints 0
}
}

i hope the problem gets solved now
Regards
Simon
Roy Simon
Ranch Hand

Joined: Aug 26, 2005
Posts: 62
hi guys
just a small correction

the statement
new superClass().i;
will not compile cos the subclass is in a different package then the super class and hence the protected member of the super class cannot be accessed by using an object of the superclass outside the superclass's package...

regards
Simon
Rishi Chopra
Ranch Hand

Joined: Mar 07, 2004
Posts: 108
hi simon,

but the line

----
System.out.println(new subClass().i); will compile fine u try out..

--
Regards
Rishi
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: confusion of protected