• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

about protected static members

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I am a bit confused about the following:

What is happening between static and protected modifiers of int b?
Thanks
 
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


What is happening between static and protected modifiers of int b?


What do you mean by what is happening??
 
Panos Liaskos
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vicken,
what I mean is:
1. the above code compiles
2. if compiled with line 3 of pac2 package as:
protected int b;
compilation fails (expected),
and the output is: b has protected access in pac2.classB

My question is why is this happening?
Why if you mark int b as static we have no error about the protected modifier?
Thanks
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Class B cannot access protected instance fields or methods of A through references of type A. (see JLS 6.6.2.1)
Here is the reason given by Arnold, Gosling & Holmes in The Java Programming Language, 3.5 What protected Really Means


The reason behind the restriction is this: Each subclass inherits the contract of the superclass and expands that contract in someway. Suppose that one subclass, as part of its expanded contract, places constraints on the values of protected members of the superclass. If a different subclass could access the protected members of objects of the first class then it could manipulate them in a way that would break the first class�s contract�and this should not be permissible.
Protected static members can be accessed in any extended class...This is allowed because a subclass can�t modify the contract of its static members as it can only hide them, not override them�hence there is no danger of another class violating that contract.

 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Once before, when I quoted this passage from TJPL, the person with the question found the bit about "constraints" a little vague. Instead of repeating the very nice but long example on queues given in TJPL, I tried to capture the essence of the example in a shorter one Here see the reply at September 02, 2003 11:59 AM
 
Panos Liaskos
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marlene,
Thanks for your replies.
You have been very helpful.
Thanks
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Panos. The more I think about it, I cannot say that I understand the explanation about static members given in TJPL. However, since Gosling is a coauthor of that book, that is the explanation we must try to understand.
 
Panos Liaskos
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marlene,
The only way I can get through this is the fact that there is always only one copy of the variable, marked with the static modifier, for the class or the subclasses instances.
So, any manipulation by any subclass would not break the contract.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This code could explain the intention of Gosling

Only a compilation error is produced:
b\Test.java:7: i has protected access in a.Base
System.out.println(b.i);//1

If we place the three classes in the same package, and eliminate package and import sentences the output is 1 and 2.
In class Base i and j values are between 1 and 10. Derived class contraints these values to 3-7. A third class Test can use consitently a static field j. However it cannot use consistently the instance field i.
This situation is prevented by the protected modifier when the classes are placed in different packages. Regarding the case where all the classes are in the same package, a programmer that releases a package should know that derived classes cannot strengthen the preconditions of methods in the base class. The risk is to break polymorphism.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What a nice example, Jose. I see how the Derived class constrains the values of the inherited fields. I understand the mechanics of the language.
However I don�t understand why the rules are different for static fields and instance fields. Why should the Test subclass be allowed to access the protected static field of the Derived class?
 
Author
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I feel compelled to jump into the fray here. This is an excellent question and the answer is that the meaning of protected modifier depends on what is modified. (It is very much like the static modifier in that regard.) In short, the protected modifer has several different meanings, and you will search in vain for a single definition. That may sound like a simplistic answer, but I believe it is the most direct. When modifying a static field or method, protected has what I describe as the "basic meaning of protected" which is what you will find in most textbooks; namely, that the field or method is accessible to subclasses declared in other packages. The basic meaning of protected also applies to types. When modifying instance variables and methods, instances of the superclass are being literally "protected" from those same subclasses (who inherit the member but cannot access it in a superclass instance). Constructors are a whole other matter. The fact is that the protected modifer is one of the most difficult aspects of the language to truly understand. In my 2.10.1 The protected Access Modifier I have no less than ten pages, partly because I analize the JLS approach to the subject.
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all
I understand the example by Jose and that clears me up on the concept. I knew how protected works but I really never give thoughts to "why it works this way".
Thanks Jose.
Regards
Maulin
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Marlene,


Why should the Test subclass be allowed to access the protected static field of the Derived class?


They are both in the same package. The access to its protected fields is ok.
Notice that in this example if we comment out "System.out.println(b.i);//1" the static method called is the one declared in Base.
The question could be why the Test subclass is allowed to access protected static fields declared in its Base class?
The protected modifier is telling to the subclasses of the declaring class, hey, you will receive your own copy of my protected instance fields whatever package you were declared. But it also adds, you won't be able to access the protected instance fields in objects of my own type, in the case you were declared in a package different than mine. Doing so, the class declaring protected instance fields is protecting the state of its instances from being manipulated in another package. Now, static fields do not compound the state of an object. The protected modifier has nothing to protect. Static fields are shared between many objects, some of them are subclasses of the declaring class, even though they were placed in another package.
 
reply
    Bookmark Topic Watch Topic
  • New Topic