• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

confusion of protected

 
Rishi Chopra
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ryan Kade
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rishi Chopra
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1589
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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




Tx
 
lexander Bosco
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kathy thanks for the refreshment on that.
 
srikanth reddy
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Simon
 
Rishi Chopra
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi simon,

but the line

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

--
Regards
Rishi
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic