This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

protected access  RSS feed

 
shambhavi sham
Ranch Hand
Posts: 139
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



in the above code, floatInWater() and text are protected members in the Bird class.
i understand that there is a rule which restricts the kind of access on lines 15 and 16 .

but what is the purpose of having such a restriction i.e Bird reference not being able to access floatInWater() and text from its subclass ?
why isn't this allowed , when it appears to make perfect sense ?
 
shambhavi sham
Ranch Hand
Posts: 139
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i get it that Bird is from a different package and the Bird reference isn't a subclass and hence, the compiler error !

but why does it have to be this way ??
 
Henry Wong
author
Sheriff
Posts: 23275
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shambhavi sham wrote:
but what is the purpose of having such a restriction i.e Bird reference not being able to access floatInWater() and text from its subclass ?
why isn't this allowed , when it appears to make perfect sense ?


Question: Do you believe that a class, that does *not* subclass your class, should be able to access your class' protected fields?

For example, what's preventing me from doing this?



In other words, while it may make "perfect sense" for the Bird class (debatable), does it also make sense for all other classes that are IS-A Bird?

Henry
 
shambhavi sham
Ranch Hand
Posts: 139
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ya there's nothing wrong right ?    

they all anyways inherit Bird's members. So how does it matter

i'm a little confused since i'm in the exploring stage

so can you please give me the explanation clearly?
 
Henry Wong
author
Sheriff
Posts: 23275
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shambhavi sham wrote:
so can you please give me the explanation clearly?


Don't know what to tell you, except ... if you believe that a class that is not in your class hierarchy, should have access to protected members in your hierarchy (because it uses the same super class), then I guess we have to agree to disagree. Sorry.

Henry
 
shambhavi sham
Ranch Hand
Posts: 139
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
     


in this case, the protected members being accessed are the same.

why do you feel that its not correct 
 
shambhavi sham
Ranch Hand
Posts: 139
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
its just the location that differs right

the swan object can anyways access Bird's protected members.
instead of accessing them from its class, its doing so from the crow class. its just using something that belongs to it, and whether it does it from its hierarchy or from another hierarchy, the fact that it can access Bird's protected members remains valid only !

even if it is not allowed into another hierarchy, Bird's protected members will be visible to it, so whats wrong then ?
 
Henry Wong
author
Sheriff
Posts: 23275
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shambhavi sham wrote:
why do you feel that its not correct 


It make sense to me. Without this feature, there is no way for me to protect my protected superclass members, even if I declare my subclass as final, and place my code in a sealed jar file.

And regardless ... there is no advantage of either of us, trying to convince the other, as it is what it is. I agree with it. You don't. We simply have to agree to disagree. Sorry.

Henry
 
Roel De Nijs
Sheriff
Posts: 11333
177
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shambhavi sham wrote:but what is the purpose of having such a restriction i.e Bird reference not being able to access floatInWater() and text from its subclass ?
why isn't this allowed , when it appears to make perfect sense ?

Very simple: if you want other classes to access your method, just mark it as public and you don't have any restrictions or issues at all

If you are designing a (framework) API, you might want to protect access to certain methods of any class because these methods should only be invoked through other methods from the same class or subclasses (it makes no sense to invoke these methods directly from another class) and that's why you'll mark these methods as protected.
 
Henry Wong
author
Sheriff
Posts: 23275
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shambhavi sham wrote:
i understand that there is a rule which restricts the kind of access on lines 15 and 16 .


Just as an FYI for the readers of this topic... The OP and I are debating this rule...

http://docs.oracle.com/javase/specs/jls/se7/html/jls-6.html#jls-6.6.2

... which is in addition to the definition of what is protected access.

Henry
 
shambhavi sham
Ranch Hand
Posts: 139
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no no henry  i am no one to debate with experts !   

i am learning and exploring now so just getting my doubts cleared  .

thanks to all who have provided their valuable answers ! 

 
Henry Wong
author
Sheriff
Posts: 23275
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shambhavi sham wrote:its just the location that differs right

the swan object can anyways access Bird's protected members.
instead of accessing them from its class, its doing so from the crow class. its just using something that belongs to it, and whether it does it from its hierarchy or from another hierarchy, the fact that it can access Bird's protected members remains valid only !

even if it is not allowed into another hierarchy, Bird's protected members will be visible to it, so whats wrong then ?


I am going to try this argument one more time....

Let's say the Bird class and the Swan class are done by different developers. This is actually reasonable as they are in different packages. So, the application / service creates Swan classes, and uses it internally. And of course, you are the developer of the Swan class.

And let's say, I am some sort of hacker, and for some reason I can hack into your service with some sort of code, that can see some of the Swan instances, but can't really replace them.

So, I want to mess you up -- as that is the hacker thing to do. So, I create a Crow class... whose job is to change the Swan class internal variables.

Anyway, you are saying that my Crow class, since it subclass from the Bird class, should be able to change the Bird class protected variables, even the protected variables in Swan instances, for which I have no access rights to?  I agree that the Crow class should be able to change the Bird class protected variables, but it should be for Crow instances only -- changing it for any class that can subclass Bird, well, isn't that a security hole?

Henry


 
shambhavi sham
Ranch Hand
Posts: 139
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
crow too inherits from Bird. so when crow calls the protected members of Bird from inside the swan class, its going to call the same version of method that will be used by Swan too , since no overriding is done here. and whatever protected variables it would use in the process, will also be for the crow's copy of those variables and not the swan's.  Its just that crow is calling Bird's protected members from inside the Swan class, but it will be operating only on its members right ? So then whats wrong?
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 4081
33
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The confusion arises because inheritance and access are two different things but are mixed together through the accessibility keywords i.e. public, protected, private in a not so intuitive fashion.

For example, the keyword public implies two things - subclass inherits it and the whole world can access it.

protected implies two things - subclass inherits it but only that particular subclass can access it - This is the situation you are confused about. This is specifically meant for the cases where only the subclass should know when to make use of the inherited members. Don't look at it as a restriction. "protected" is an additional feature between "public" and "private", which lets a subclass inherit from a parent but in a "private" fashion. There could have been another keyword (may be "unprotected") between public and private, which could have let a subclass inherit from a parent and also let other subclasses access that member. But the language designers probably thought that it would not be either too useful or too safe.

Think of protected inheritance this way - just because you and your sibling inherited something from your parents, does it mean that you should be able to access your sibling's stuff? If you inherited something, it is your stuff. Not someone else's. protected is like your dad gave you a gift without telling your bro
 
Henry Wong
author
Sheriff
Posts: 23275
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Anilprem wrote:
Think of protected inheritance this way - just because you and your sibling inherited something from your parents, does it mean that you should be able to access your sibling's stuff? If you inherited something, it is your stuff. Not someone else's. protected is like your dad gave you a gift without telling your bro


I like this. Have a cow...

Henry
 
shambhavi sham
Ranch Hand
Posts: 139
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not even saying that other sibling classes should be able to access their sibling's protected members !

i'm saying this :

class parent {

protected int a=9;
}

class child1 extends parent {
public static void main(String[] args)
{

parent p=new parent();
parent c1=new child1();
parent c2=new child2();

System.out.println(c1.a);
System.out.println(c2.a);
c1.a=10;
c2.a=11;
System.out.println( c1.a);
System.out.println( c2.a);


}

}

class child2 extends parent {

}

when all these classes are in the same package, this will give the output :
8
8
10
11

each class is just using its version of the protected member that it has inherited ! even when changes are made to c2.a, c1.a is not affected, only c2's version of its variable 'a' is changed. so using c2 which is the sibling of c1, we are not able to change c1's a.

so when the same is executed with these classes being from different packages, whats hampering the security ? why does this become not possible to do it from different packages ?


 
Henry Wong
author
Sheriff
Posts: 23275
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shambhavi sham wrote:crow too inherits from Bird. so when crow calls the protected members of Bird from inside the swan class, its going to call the same version of method that will be used by Swan too , since no overriding is done here. and whatever protected variables it would use in the process, will also be for the crow's copy of those variables and not the swan's.  Its just that crow is calling Bird's protected members from inside the Swan class, but it will be operating only on its members right ? So then whats wrong?


No. It will be operating based on the reference that is used.

If the code in the hacking Crow class uses a Swan reference to access an instance variable, it will be the instance variable of the Swan instance pointed to by the reference that will be accessed (or changed). If the hacking code uses a Swan reference to call an instance method, it will be the instance method of the Swan instance too. And if this Swan instance method, uses its this reference, then it will change itself (thinking that the caller is trusted, because the method is protected).

Henry
 
shambhavi sham
Ranch Hand
Posts: 139
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shambhavi sham wrote:I'm not even saying that other sibling classes should be able to access their sibling's protected members !

i'm saying this :

class parent {

protected int a=9;
}

class child1 extends parent {
public static void main(String[] args)
{

parent p=new parent();
parent c1=new child1();
parent c2=new child2();

System.out.println(c1.a);
System.out.println(c2.a);
c1.a=10;
c2.a=11;
System.out.println( c1.a);
System.out.println( c2.a);


}

}

class child2 extends parent {

}

when all these classes are in the same package, this will give the output :
8
8
10
11

each class is just using its version of the protected member that it has inherited ! even when changes are made to c2.a, c1.a is not affected, only c2's version of its variable 'a' is changed. so using c2 which is the sibling of c1, we are not able to change c1's a.

so when the same is executed with these classes being from different packages, whats hampering the security ? why does this become not possible to do it from different packages ?






i am only discussing about this context Henry ! 
 
Henry Wong
author
Sheriff
Posts: 23275
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shambhavi sham wrote:
each class is just using its version of the protected member that it has inherited ! even when changes are made to c2.a, c1.a is not affected, only c2's version of its variable 'a' is changed. so using c2 which is the sibling of c1, we are not able to change c1's a.


Not true. The reason Child2 is not changing Child1 instances, is because there is no code in Child2 that does so.

On the other hand, code in Child1 is changing references of Child2 -- ie. the main() method of Child1, is changing the protected variable of Child2 instances. Same package issue aside, which I will address next, think of Child2 as the Swan, and Child1 as the Crow. Child1 is able to hack into Child2, and access his/her stuff.

Henry
 
Henry Wong
author
Sheriff
Posts: 23275
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shambhavi sham wrote:so when the same is executed with these classes being from different packages, whats hampering the security ? why does this become not possible to do it from different packages ?


This is because there are *four* access levels. The relevant one here, aka. default or "package private" level, is between the private and protected access levels. This gives classes in the same package access to each other.  And since the protected access level is more accessible that the default, protected also means that classes in the same package can access the protected fields.

In my humble opinion, I still think that this was a bad idea. The C++ language provides this access via the use of friend classes, which gives much more fine grained, and individual control of the access. The Java designers probably thought that this was a complicated idea, and merged the concepts into this simpler form. Unfortunately, it is what it is ...

Henry
 
shambhavi sham
Ranch Hand
Posts: 139
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
shambhavi sham wrote:
each class is just using its version of the protected member that it has inherited ! even when changes are made to c2.a, c1.a is not affected, only c2's version of its variable 'a' is changed. so using c2 which is the sibling of c1, we are not able to change c1's a.


Not true. The reason Child2 is not changing Child1 instances, is because there is no code in Child2 that does so.

On the other hand, code in Child1 is changing references of Child2 -- ie. the main() method of Child1, is changing the protected variable of Child2 instances. Same package issue aside, which I will address next, think of Child2 as the Swan, and Child1 as the Crow. Child1 is able to hack into Child2, and access his/her stuff.

Henry



ummm...... OKAY i feel a bit better and a little convinced ! The ranch is indeed a good place to do all this sort of research kind of stuff !
 
Henry Wong
author
Sheriff
Posts: 23275
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shambhavi sham wrote:
i am only discussing about this context Henry ! 


Question. Do you no longer care about the previous context because you understood it? Or do you no longer care about the previous context because you no longer care about learning the subject in that context?

For the first, great. For the second, why would you not care anymore?

Henry
 
shambhavi sham
Ranch Hand
Posts: 139
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Anilprem wrote:The confusion arises because inheritance and access are two different things but are mixed together through the accessibility keywords i.e. public, protected, private in a not so intuitive fashion.

For example, the keyword public implies two things - subclass inherits it and the whole world can access it.

protected implies two things - subclass inherits it but only that particular subclass can access it - This is the situation you are confused about. This is specifically meant for the cases where only the subclass should know when to make use of the inherited members. Don't look at it as a restriction. "protected" is an additional feature between "public" and "private", which lets a subclass inherit from a parent but in a "private" fashion. There could have been another keyword (may be "unprotected") between public and private, which could have let a subclass inherit from a parent and also let other subclasses access that member. But the language designers probably thought that it would not be either too useful or too safe.

Think of protected inheritance this way - just because you and your sibling inherited something from your parents, does it mean that you should be able to access your sibling's stuff? If you inherited something, it is your stuff. Not someone else's. protected is like your dad gave you a gift without telling your bro


thanks paul ! good one !  
 
shambhavi sham
Ranch Hand
Posts: 139
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
shambhavi sham wrote:crow too inherits from Bird. so when crow calls the protected members of Bird from inside the swan class, its going to call the same version of method that will be used by Swan too , since no overriding is done here. and whatever protected variables it would use in the process, will also be for the crow's copy of those variables and not the swan's.  Its just that crow is calling Bird's protected members from inside the Swan class, but it will be operating only on its members right ? So then whats wrong?


No. It will be operating based on the reference that is used.

If the code in the hacking Crow class uses a Swan reference to access an instance variable, it will be the instance variable of the Swan instance pointed to by the reference that will be accessed (or changed). If the hacking code uses a Swan reference to call an instance method, it will be the instance method of the Swan instance too. And if this Swan instance method, uses its this reference, then it will change itself (thinking that the caller is trusted, because the method is protected).

Henry


isn't this one and my child1 child2 similar ? crow being child1 and swan being child2 as you had said . which other context was raised in the discussion ?
 
Henry Wong
author
Sheriff
Posts: 23275
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shambhavi sham wrote:
isn't this one and my child1 child2 similar ? crow being child1 and swan being child2 as you had said . which other context was raised in the discussion ?


Yes. It is similar -- except for the change of packages (to the same package).... but this was not my concern. My concern was, your change in context, and refusal to stay with the previous context, was also a refusal to address the last response as well.

Henry
 
shambhavi sham
Ranch Hand
Posts: 139
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
uh nono ! i have understood about why it should be the way it is in case of different packages ! and i once again thank you henry , paul and everybody for their responses !

i am coming up with a new question though in another thread soon !
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!